2010-12-02 20:48:45 +00:00
|
|
|
/*
|
|
|
|
* Cogl
|
|
|
|
*
|
|
|
|
* An object oriented GL/GLES Abstraction/Utility Layer
|
|
|
|
*
|
2013-02-26 17:52:20 +00:00
|
|
|
* Copyright (C) 2010,2013 Intel Corporation.
|
2010-12-02 20:48:45 +00:00
|
|
|
*
|
|
|
|
* 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:
|
|
|
|
* Neil Roberts <neil@linux.intel.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2011-11-25 15:31:21 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2013-06-20 12:25:49 +00:00
|
|
|
#include <test-fixtures/test-unit.h>
|
|
|
|
|
2011-09-23 08:43:05 +00:00
|
|
|
#include "cogl-context-private.h"
|
2013-01-20 18:47:40 +00:00
|
|
|
#include "cogl-util-gl-private.h"
|
2010-12-02 20:48:45 +00:00
|
|
|
#include "cogl-pipeline-private.h"
|
|
|
|
#include "cogl-pipeline-opengl-private.h"
|
|
|
|
|
|
|
|
#ifdef COGL_PIPELINE_VERTEND_GLSL
|
|
|
|
|
2010-11-04 22:25:52 +00:00
|
|
|
#include "cogl-context-private.h"
|
2012-04-16 13:14:10 +00:00
|
|
|
#include "cogl-object-private.h"
|
2010-12-02 20:48:45 +00:00
|
|
|
#include "cogl-program-private.h"
|
|
|
|
#include "cogl-pipeline-vertend-glsl-private.h"
|
2011-11-29 14:21:07 +00:00
|
|
|
#include "cogl-pipeline-state-private.h"
|
2013-01-07 19:16:11 +00:00
|
|
|
#include "cogl-glsl-shader-private.h"
|
2010-12-02 20:48:45 +00:00
|
|
|
|
|
|
|
const CoglPipelineVertend _cogl_pipeline_glsl_vertend;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
unsigned int ref_count;
|
|
|
|
|
|
|
|
GLuint gl_shader;
|
|
|
|
GString *header, *source;
|
|
|
|
|
2011-06-30 12:39:48 +00:00
|
|
|
} CoglPipelineShaderState;
|
2010-12-02 20:48:45 +00:00
|
|
|
|
2011-06-30 12:39:48 +00:00
|
|
|
static CoglUserDataKey shader_state_key;
|
2010-12-02 20:48:45 +00:00
|
|
|
|
2011-06-30 12:39:48 +00:00
|
|
|
static CoglPipelineShaderState *
|
|
|
|
shader_state_new (void)
|
2010-12-02 20:48:45 +00:00
|
|
|
{
|
2011-06-30 12:39:48 +00:00
|
|
|
CoglPipelineShaderState *shader_state;
|
|
|
|
|
|
|
|
shader_state = g_slice_new0 (CoglPipelineShaderState);
|
|
|
|
shader_state->ref_count = 1;
|
|
|
|
|
|
|
|
return shader_state;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CoglPipelineShaderState *
|
|
|
|
get_shader_state (CoglPipeline *pipeline)
|
|
|
|
{
|
|
|
|
return cogl_object_get_user_data (COGL_OBJECT (pipeline), &shader_state_key);
|
2010-12-02 20:48:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-06-30 12:39:48 +00:00
|
|
|
destroy_shader_state (void *user_data)
|
2010-12-02 20:48:45 +00:00
|
|
|
{
|
2011-06-30 12:39:48 +00:00
|
|
|
CoglPipelineShaderState *shader_state = user_data;
|
2010-12-02 20:48:45 +00:00
|
|
|
|
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
2011-06-30 12:39:48 +00:00
|
|
|
if (--shader_state->ref_count == 0)
|
2010-12-02 20:48:45 +00:00
|
|
|
{
|
2011-06-30 12:39:48 +00:00
|
|
|
if (shader_state->gl_shader)
|
|
|
|
GE( ctx, glDeleteShader (shader_state->gl_shader) );
|
2010-12-02 20:48:45 +00:00
|
|
|
|
2011-06-30 12:39:48 +00:00
|
|
|
g_slice_free (CoglPipelineShaderState, shader_state);
|
2010-12-02 20:48:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-06-30 12:39:48 +00:00
|
|
|
set_shader_state (CoglPipeline *pipeline,
|
|
|
|
CoglPipelineShaderState *shader_state)
|
2010-12-02 20:48:45 +00:00
|
|
|
{
|
|
|
|
cogl_object_set_user_data (COGL_OBJECT (pipeline),
|
2011-06-30 12:39:48 +00:00
|
|
|
&shader_state_key,
|
|
|
|
shader_state,
|
|
|
|
destroy_shader_state);
|
2010-12-02 20:48:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-06-30 12:39:48 +00:00
|
|
|
dirty_shader_state (CoglPipeline *pipeline)
|
2010-12-02 20:48:45 +00:00
|
|
|
{
|
|
|
|
cogl_object_set_user_data (COGL_OBJECT (pipeline),
|
2011-06-30 12:39:48 +00:00
|
|
|
&shader_state_key,
|
2010-12-02 20:48:45 +00:00
|
|
|
NULL,
|
2011-06-30 12:39:48 +00:00
|
|
|
NULL);
|
2010-12-02 20:48:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GLuint
|
|
|
|
_cogl_pipeline_vertend_glsl_get_shader (CoglPipeline *pipeline)
|
|
|
|
{
|
2011-06-30 12:39:48 +00:00
|
|
|
CoglPipelineShaderState *shader_state = get_shader_state (pipeline);
|
2010-12-02 20:48:45 +00:00
|
|
|
|
2011-06-30 12:39:48 +00:00
|
|
|
if (shader_state)
|
|
|
|
return shader_state->gl_shader;
|
2010-12-02 20:48:45 +00:00
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-17 16:52:21 +00:00
|
|
|
static CoglPipelineSnippetList *
|
|
|
|
get_vertex_snippets (CoglPipeline *pipeline)
|
|
|
|
{
|
|
|
|
pipeline =
|
|
|
|
_cogl_pipeline_get_authority (pipeline,
|
|
|
|
COGL_PIPELINE_STATE_VERTEX_SNIPPETS);
|
|
|
|
|
|
|
|
return &pipeline->big_state->vertex_snippets;
|
|
|
|
}
|
|
|
|
|
2011-11-28 19:58:15 +00:00
|
|
|
static CoglPipelineSnippetList *
|
|
|
|
get_layer_vertex_snippets (CoglPipelineLayer *layer)
|
|
|
|
{
|
|
|
|
unsigned long state = COGL_PIPELINE_LAYER_STATE_VERTEX_SNIPPETS;
|
|
|
|
layer = _cogl_pipeline_layer_get_authority (layer, state);
|
|
|
|
|
|
|
|
return &layer->big_state->vertex_snippets;
|
|
|
|
}
|
|
|
|
|
2013-02-26 18:29:36 +00:00
|
|
|
static CoglBool
|
|
|
|
add_layer_declaration_cb (CoglPipelineLayer *layer,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
CoglPipelineShaderState *shader_state = user_data;
|
|
|
|
CoglTextureType texture_type =
|
|
|
|
_cogl_pipeline_layer_get_texture_type (layer);
|
|
|
|
const char *target_string;
|
|
|
|
|
|
|
|
_cogl_gl_util_get_texture_target_string (texture_type, &target_string, NULL);
|
|
|
|
|
|
|
|
g_string_append_printf (shader_state->header,
|
|
|
|
"uniform sampler%s cogl_sampler%i;\n",
|
|
|
|
target_string,
|
|
|
|
layer->index);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
add_layer_declarations (CoglPipeline *pipeline,
|
|
|
|
CoglPipelineShaderState *shader_state)
|
|
|
|
{
|
|
|
|
/* We always emit sampler uniforms in case there will be custom
|
|
|
|
* layer snippets that want to sample arbitrary layers. */
|
|
|
|
|
|
|
|
_cogl_pipeline_foreach_layer_internal (pipeline,
|
|
|
|
add_layer_declaration_cb,
|
|
|
|
shader_state);
|
|
|
|
}
|
|
|
|
|
2013-02-26 17:52:20 +00:00
|
|
|
static void
|
|
|
|
add_global_declarations (CoglPipeline *pipeline,
|
|
|
|
CoglPipelineShaderState *shader_state)
|
|
|
|
{
|
|
|
|
CoglSnippetHook hook = COGL_SNIPPET_HOOK_VERTEX_GLOBALS;
|
|
|
|
CoglPipelineSnippetList *snippets = get_vertex_snippets (pipeline);
|
|
|
|
|
|
|
|
/* Add the global data hooks. All of the code in these snippets is
|
|
|
|
* always added and only the declarations data is used */
|
|
|
|
|
|
|
|
_cogl_pipeline_snippet_generate_declarations (shader_state->header,
|
|
|
|
hook,
|
|
|
|
snippets);
|
|
|
|
}
|
|
|
|
|
2012-09-25 20:08:10 +00:00
|
|
|
static void
|
2010-12-02 20:48:45 +00:00
|
|
|
_cogl_pipeline_vertend_glsl_start (CoglPipeline *pipeline,
|
|
|
|
int n_layers,
|
2012-09-27 10:06:16 +00:00
|
|
|
unsigned long pipelines_difference)
|
2010-12-02 20:48:45 +00:00
|
|
|
{
|
2011-06-30 12:39:48 +00:00
|
|
|
CoglPipelineShaderState *shader_state;
|
2011-06-30 17:34:05 +00:00
|
|
|
CoglPipeline *template_pipeline = NULL;
|
2012-09-25 20:08:10 +00:00
|
|
|
CoglProgram *user_program = cogl_pipeline_get_user_program (pipeline);
|
2010-12-02 20:48:45 +00:00
|
|
|
|
2012-09-25 20:08:10 +00:00
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
2010-12-02 20:48:45 +00:00
|
|
|
|
|
|
|
/* Now lookup our glsl backend private state (allocating if
|
|
|
|
* necessary) */
|
2011-06-30 12:39:48 +00:00
|
|
|
shader_state = get_shader_state (pipeline);
|
2010-12-02 20:48:45 +00:00
|
|
|
|
2011-06-30 12:39:48 +00:00
|
|
|
if (shader_state == NULL)
|
2010-12-02 20:48:45 +00:00
|
|
|
{
|
|
|
|
CoglPipeline *authority;
|
|
|
|
|
|
|
|
/* Get the authority for anything affecting vertex shader
|
|
|
|
state */
|
|
|
|
authority = _cogl_pipeline_find_equivalent_parent
|
|
|
|
(pipeline,
|
2013-06-20 12:25:49 +00:00
|
|
|
_cogl_pipeline_get_state_for_vertex_codegen (ctx) &
|
2010-12-02 20:48:45 +00:00
|
|
|
~COGL_PIPELINE_STATE_LAYERS,
|
2011-01-11 16:02:06 +00:00
|
|
|
COGL_PIPELINE_LAYER_STATE_AFFECTS_VERTEX_CODEGEN);
|
2010-12-02 20:48:45 +00:00
|
|
|
|
2011-06-30 12:39:48 +00:00
|
|
|
shader_state = get_shader_state (authority);
|
2010-12-02 20:48:45 +00:00
|
|
|
|
2011-06-30 12:39:48 +00:00
|
|
|
if (shader_state == NULL)
|
2010-12-02 20:48:45 +00:00
|
|
|
{
|
2011-06-30 17:34:05 +00:00
|
|
|
/* Check if there is already a similar cached pipeline whose
|
|
|
|
shader state we can share */
|
|
|
|
if (G_LIKELY (!(COGL_DEBUG_ENABLED
|
|
|
|
(COGL_DEBUG_DISABLE_PROGRAM_CACHES))))
|
|
|
|
{
|
|
|
|
template_pipeline =
|
|
|
|
_cogl_pipeline_cache_get_vertex_template (ctx->pipeline_cache,
|
|
|
|
authority);
|
|
|
|
|
|
|
|
shader_state = get_shader_state (template_pipeline);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shader_state)
|
|
|
|
shader_state->ref_count++;
|
|
|
|
else
|
|
|
|
shader_state = shader_state_new ();
|
|
|
|
|
2011-06-30 12:39:48 +00:00
|
|
|
set_shader_state (authority, shader_state);
|
2011-06-30 17:34:05 +00:00
|
|
|
|
|
|
|
if (template_pipeline)
|
|
|
|
{
|
|
|
|
shader_state->ref_count++;
|
|
|
|
set_shader_state (template_pipeline, shader_state);
|
|
|
|
}
|
2010-12-02 20:48:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (authority != pipeline)
|
|
|
|
{
|
2011-06-30 12:39:48 +00:00
|
|
|
shader_state->ref_count++;
|
|
|
|
set_shader_state (pipeline, shader_state);
|
2010-12-02 20:48:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user_program)
|
2012-09-27 10:06:16 +00:00
|
|
|
{
|
|
|
|
/* If the user program contains a vertex shader then we don't need
|
|
|
|
to generate one */
|
|
|
|
if (_cogl_program_has_vertex_shader (user_program))
|
2013-01-19 16:00:33 +00:00
|
|
|
{
|
|
|
|
if (shader_state->gl_shader)
|
|
|
|
{
|
|
|
|
GE( ctx, glDeleteShader (shader_state->gl_shader) );
|
|
|
|
shader_state->gl_shader = 0;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2012-09-27 10:06:16 +00:00
|
|
|
}
|
2010-12-02 20:48:45 +00:00
|
|
|
|
2013-01-19 16:00:33 +00:00
|
|
|
if (shader_state->gl_shader)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* If we make it here then we have a 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-12-02 20:48:45 +00: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 */
|
|
|
|
g_string_set_size (ctx->codegen_header_buffer, 0);
|
|
|
|
g_string_set_size (ctx->codegen_source_buffer, 0);
|
2011-06-30 12:39:48 +00:00
|
|
|
shader_state->header = ctx->codegen_header_buffer;
|
|
|
|
shader_state->source = ctx->codegen_source_buffer;
|
2010-12-02 20:48:45 +00:00
|
|
|
|
2013-02-26 18:29:36 +00:00
|
|
|
add_layer_declarations (pipeline, shader_state);
|
2013-02-26 17:52:20 +00:00
|
|
|
add_global_declarations (pipeline, shader_state);
|
|
|
|
|
2011-06-30 12:39:48 +00:00
|
|
|
g_string_append (shader_state->source,
|
2010-12-02 20:48:45 +00:00
|
|
|
"void\n"
|
2011-11-25 13:41:13 +00:00
|
|
|
"cogl_generated_source ()\n"
|
2010-12-02 20:48:45 +00:00
|
|
|
"{\n");
|
|
|
|
|
2012-11-08 16:56:02 +00:00
|
|
|
if (cogl_pipeline_get_per_vertex_point_size (pipeline))
|
|
|
|
g_string_append (shader_state->header,
|
|
|
|
"attribute float cogl_point_size_in;\n");
|
|
|
|
else if (!(ctx->private_feature_flags &
|
|
|
|
COGL_PRIVATE_FEATURE_BUILTIN_POINT_SIZE_UNIFORM))
|
|
|
|
{
|
|
|
|
/* There is no builtin uniform for the point size on GLES2 so we
|
2013-06-20 12:25:49 +00:00
|
|
|
need to copy it from the custom uniform in the vertex shader
|
|
|
|
if we're not using per-vertex point sizes, however we'll only
|
|
|
|
do this if the point-size is non-zero. Toggle the point size
|
|
|
|
between zero and non-zero causes a state change which
|
|
|
|
generates a new program */
|
|
|
|
if (cogl_pipeline_get_point_size (pipeline) > 0.0f)
|
|
|
|
{
|
|
|
|
g_string_append (shader_state->header,
|
|
|
|
"uniform float cogl_point_size_in;\n");
|
|
|
|
g_string_append (shader_state->source,
|
|
|
|
" cogl_point_size_out = cogl_point_size_in;\n");
|
|
|
|
}
|
2012-11-08 16:56:02 +00:00
|
|
|
}
|
2010-12-02 20:48:45 +00:00
|
|
|
}
|
|
|
|
|
Switch use of primitive glib types to c99 equivalents
The coding style has for a long time said to avoid using redundant glib
data types such as gint or gchar etc because we feel that they make the
code look unnecessarily foreign to developers coming from outside of the
Gnome developer community.
Note: When we tried to find the historical rationale for the types we
just found that they were apparently only added for consistent syntax
highlighting which didn't seem that compelling.
Up until now we have been continuing to use some of the platform
specific type such as gint{8,16,32,64} and gsize but this patch switches
us over to using the standard c99 equivalents instead so we can further
ensure that our code looks familiar to the widest range of C developers
who might potentially contribute to Cogl.
So instead of using the gint{8,16,32,64} and guint{8,16,32,64} types this
switches all Cogl code to instead use the int{8,16,32,64}_t and
uint{8,16,32,64}_t c99 types instead.
Instead of gsize we now use size_t
For now we are not going to use the c99 _Bool type and instead we have
introduced a new CoglBool type to use instead of gboolean.
Reviewed-by: Neil Roberts <neil@linux.intel.com>
(cherry picked from commit 5967dad2400d32ca6319cef6cb572e81bf2c15f0)
2012-04-16 20:56:40 +00:00
|
|
|
static CoglBool
|
2010-12-02 20:48:45 +00:00
|
|
|
_cogl_pipeline_vertend_glsl_add_layer (CoglPipeline *pipeline,
|
|
|
|
CoglPipelineLayer *layer,
|
Re-design the matrix stack using a graph of ops
This re-designs the matrix stack so we now keep track of each separate
operation such as rotating, scaling, translating and multiplying as
immutable, ref-counted nodes in a graph.
Being a "graph" here means that different transformations composed of
a sequence of linked operation nodes may share nodes.
The first node in a matrix-stack is always a LOAD_IDENTITY operation.
As an example consider if an application where to draw three rectangles
A, B and C something like this:
cogl_framebuffer_scale (fb, 2, 2, 2);
cogl_framebuffer_push_matrix(fb);
cogl_framebuffer_translate (fb, 10, 0, 0);
cogl_framebuffer_push_matrix(fb);
cogl_framebuffer_rotate (fb, 45, 0, 0, 1);
cogl_framebuffer_draw_rectangle (...); /* A */
cogl_framebuffer_pop_matrix(fb);
cogl_framebuffer_draw_rectangle (...); /* B */
cogl_framebuffer_pop_matrix(fb);
cogl_framebuffer_push_matrix(fb);
cogl_framebuffer_set_modelview_matrix (fb, &mv);
cogl_framebuffer_draw_rectangle (...); /* C */
cogl_framebuffer_pop_matrix(fb);
That would result in a graph of nodes like this:
LOAD_IDENTITY
|
SCALE
/ \
SAVE LOAD
| |
TRANSLATE RECTANGLE(C)
| \
SAVE RECTANGLE(B)
|
ROTATE
|
RECTANGLE(A)
Each push adds a SAVE operation which serves as a marker to rewind too
when a corresponding pop is issued and also each SAVE node may also
store a cached matrix representing the composition of all its ancestor
nodes. This means if we repeatedly need to resolve a real CoglMatrix
for a given node then we don't need to repeat the composition.
Some advantages of this design are:
- A single pointer to any node in the graph can now represent a
complete, immutable transformation that can be logged for example
into a journal. Previously we were storing a full CoglMatrix in
each journal entry which is 16 floats for the matrix itself as well
as space for flags and another 16 floats for possibly storing a
cache of the inverse. This means that we significantly reduce
the size of the journal when drawing lots of primitives and we also
avoid copying over 128 bytes per entry.
- It becomes much cheaper to check for equality. In cases where some
(unlikely) false negatives are allowed simply comparing the pointers
of two matrix stack graph entries is enough. Previously we would use
memcmp() to compare matrices.
- It becomes easier to do comparisons of transformations. By looking
for the common ancestry between nodes we can determine the operations
that differentiate the transforms and use those to gain a high level
understanding of the differences. For example we use this in the
journal to be able to efficiently determine when two rectangle
transforms only differ by some translation so that we can perform
software clipping.
Reviewed-by: Neil Roberts <neil@linux.intel.com>
(cherry picked from commit f75aee93f6b293ca7a7babbd8fcc326ee6bf7aef)
2012-02-20 15:59:48 +00:00
|
|
|
unsigned long layers_difference,
|
|
|
|
CoglFramebuffer *framebuffer)
|
2010-12-02 20:48:45 +00:00
|
|
|
{
|
2011-06-30 12:39:48 +00:00
|
|
|
CoglPipelineShaderState *shader_state;
|
2011-11-28 19:58:15 +00:00
|
|
|
CoglPipelineSnippetData snippet_data;
|
2012-09-27 10:06:16 +00:00
|
|
|
int layer_index = layer->index;
|
2010-12-02 20:48:45 +00:00
|
|
|
|
2011-07-07 19:44:56 +00:00
|
|
|
_COGL_GET_CONTEXT (ctx, FALSE);
|
|
|
|
|
2011-06-30 12:39:48 +00:00
|
|
|
shader_state = get_shader_state (pipeline);
|
2010-12-02 20:48:45 +00:00
|
|
|
|
2011-06-30 12:39:48 +00:00
|
|
|
if (shader_state->source == NULL)
|
2010-12-02 20:48:45 +00:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
/* Transform the texture coordinates by the layer's user matrix.
|
|
|
|
*
|
|
|
|
* FIXME: this should avoid doing the transform if there is no user
|
|
|
|
* matrix set. This might need a separate layer state flag for
|
|
|
|
* whether there is a user matrix
|
|
|
|
*
|
|
|
|
* FIXME: we could be more clever here and try to detect if the
|
|
|
|
* fragment program is going to use the texture coordinates and
|
|
|
|
* avoid setting them if not
|
|
|
|
*/
|
|
|
|
|
2011-11-28 19:58:15 +00:00
|
|
|
g_string_append_printf (shader_state->header,
|
|
|
|
"vec4\n"
|
|
|
|
"cogl_real_transform_layer%i (mat4 matrix, "
|
|
|
|
"vec4 tex_coord)\n"
|
|
|
|
"{\n"
|
|
|
|
" return matrix * tex_coord;\n"
|
|
|
|
"}\n",
|
2012-09-27 10:06:16 +00:00
|
|
|
layer_index);
|
2011-11-28 19:58:15 +00:00
|
|
|
|
|
|
|
/* Wrap the layer code in any snippets that have been hooked */
|
|
|
|
memset (&snippet_data, 0, sizeof (snippet_data));
|
|
|
|
snippet_data.snippets = get_layer_vertex_snippets (layer);
|
|
|
|
snippet_data.hook = COGL_SNIPPET_HOOK_TEXTURE_COORD_TRANSFORM;
|
|
|
|
snippet_data.chain_function = g_strdup_printf ("cogl_real_transform_layer%i",
|
2012-09-27 10:06:16 +00:00
|
|
|
layer_index);
|
2011-11-28 19:58:15 +00:00
|
|
|
snippet_data.final_name = g_strdup_printf ("cogl_transform_layer%i",
|
2012-09-27 10:06:16 +00:00
|
|
|
layer_index);
|
2011-11-28 19:58:15 +00:00
|
|
|
snippet_data.function_prefix = g_strdup_printf ("cogl_transform_layer%i",
|
2012-09-27 10:06:16 +00:00
|
|
|
layer_index);
|
2011-11-28 19:58:15 +00:00
|
|
|
snippet_data.return_type = "vec4";
|
|
|
|
snippet_data.return_variable = "cogl_tex_coord";
|
|
|
|
snippet_data.return_variable_is_argument = TRUE;
|
|
|
|
snippet_data.arguments = "cogl_matrix, cogl_tex_coord";
|
|
|
|
snippet_data.argument_declarations = "mat4 cogl_matrix, vec4 cogl_tex_coord";
|
|
|
|
snippet_data.source_buf = shader_state->header;
|
|
|
|
|
|
|
|
_cogl_pipeline_snippet_generate_code (&snippet_data);
|
|
|
|
|
|
|
|
g_free ((char *) snippet_data.chain_function);
|
|
|
|
g_free ((char *) snippet_data.final_name);
|
|
|
|
g_free ((char *) snippet_data.function_prefix);
|
|
|
|
|
2011-06-30 12:39:48 +00:00
|
|
|
g_string_append_printf (shader_state->source,
|
2012-09-27 10:06:16 +00:00
|
|
|
" cogl_tex_coord%i_out = "
|
|
|
|
"cogl_transform_layer%i (cogl_texture_matrix%i,\n"
|
2011-11-28 19:58:15 +00:00
|
|
|
" "
|
|
|
|
" cogl_tex_coord%i_in);\n",
|
2012-09-27 10:06:16 +00:00
|
|
|
layer_index,
|
|
|
|
layer_index,
|
|
|
|
layer_index,
|
|
|
|
layer_index);
|
2010-12-02 20:48:45 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
Switch use of primitive glib types to c99 equivalents
The coding style has for a long time said to avoid using redundant glib
data types such as gint or gchar etc because we feel that they make the
code look unnecessarily foreign to developers coming from outside of the
Gnome developer community.
Note: When we tried to find the historical rationale for the types we
just found that they were apparently only added for consistent syntax
highlighting which didn't seem that compelling.
Up until now we have been continuing to use some of the platform
specific type such as gint{8,16,32,64} and gsize but this patch switches
us over to using the standard c99 equivalents instead so we can further
ensure that our code looks familiar to the widest range of C developers
who might potentially contribute to Cogl.
So instead of using the gint{8,16,32,64} and guint{8,16,32,64} types this
switches all Cogl code to instead use the int{8,16,32,64}_t and
uint{8,16,32,64}_t c99 types instead.
Instead of gsize we now use size_t
For now we are not going to use the c99 _Bool type and instead we have
introduced a new CoglBool type to use instead of gboolean.
Reviewed-by: Neil Roberts <neil@linux.intel.com>
(cherry picked from commit 5967dad2400d32ca6319cef6cb572e81bf2c15f0)
2012-04-16 20:56:40 +00:00
|
|
|
static CoglBool
|
2010-12-02 20:48:45 +00:00
|
|
|
_cogl_pipeline_vertend_glsl_end (CoglPipeline *pipeline,
|
|
|
|
unsigned long pipelines_difference)
|
|
|
|
{
|
2011-06-30 12:39:48 +00:00
|
|
|
CoglPipelineShaderState *shader_state;
|
2010-12-02 20:48:45 +00:00
|
|
|
|
|
|
|
_COGL_GET_CONTEXT (ctx, FALSE);
|
|
|
|
|
2011-06-30 12:39:48 +00:00
|
|
|
shader_state = get_shader_state (pipeline);
|
2010-12-02 20:48:45 +00:00
|
|
|
|
2011-06-30 12:39:48 +00:00
|
|
|
if (shader_state->source)
|
2010-12-02 20:48:45 +00:00
|
|
|
{
|
|
|
|
const char *source_strings[2];
|
|
|
|
GLint lengths[2];
|
|
|
|
GLint compile_status;
|
|
|
|
GLuint shader;
|
2011-11-25 15:31:21 +00:00
|
|
|
CoglPipelineSnippetData snippet_data;
|
2011-11-29 14:21:07 +00:00
|
|
|
CoglPipelineSnippetList *vertex_snippets;
|
2012-11-08 16:56:02 +00:00
|
|
|
CoglBool has_per_vertex_point_size =
|
|
|
|
cogl_pipeline_get_per_vertex_point_size (pipeline);
|
2010-12-02 20:48:45 +00:00
|
|
|
|
|
|
|
COGL_STATIC_COUNTER (vertend_glsl_compile_counter,
|
|
|
|
"glsl vertex compile counter",
|
|
|
|
"Increments each time a new GLSL "
|
|
|
|
"vertex shader is compiled",
|
|
|
|
0 /* no application private data */);
|
|
|
|
COGL_COUNTER_INC (_cogl_uprof_context, vertend_glsl_compile_counter);
|
|
|
|
|
2011-11-28 22:06:53 +00:00
|
|
|
g_string_append (shader_state->header,
|
|
|
|
"void\n"
|
|
|
|
"cogl_real_vertex_transform ()\n"
|
|
|
|
"{\n"
|
2010-12-02 20:48:45 +00:00
|
|
|
" cogl_position_out = "
|
|
|
|
"cogl_modelview_projection_matrix * "
|
|
|
|
"cogl_position_in;\n"
|
2011-11-28 22:06:53 +00:00
|
|
|
"}\n");
|
|
|
|
|
|
|
|
g_string_append (shader_state->source,
|
2012-11-08 16:56:02 +00:00
|
|
|
" cogl_vertex_transform ();\n");
|
|
|
|
|
|
|
|
if (has_per_vertex_point_size)
|
|
|
|
{
|
|
|
|
g_string_append (shader_state->header,
|
|
|
|
"void\n"
|
|
|
|
"cogl_real_point_size_calculation ()\n"
|
|
|
|
"{\n"
|
|
|
|
" cogl_point_size_out = cogl_point_size_in;\n"
|
|
|
|
"}\n");
|
|
|
|
g_string_append (shader_state->source,
|
|
|
|
" cogl_point_size_calculation ();\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
g_string_append (shader_state->source,
|
2010-12-02 20:48:45 +00:00
|
|
|
" cogl_color_out = cogl_color_in;\n"
|
2011-11-25 13:41:13 +00:00
|
|
|
"}\n");
|
2011-11-17 16:52:21 +00:00
|
|
|
|
2011-11-29 14:21:07 +00:00
|
|
|
vertex_snippets = get_vertex_snippets (pipeline);
|
|
|
|
|
2011-11-28 22:06:53 +00:00
|
|
|
/* Add hooks for the vertex transform part */
|
|
|
|
memset (&snippet_data, 0, sizeof (snippet_data));
|
2011-11-29 14:21:07 +00:00
|
|
|
snippet_data.snippets = vertex_snippets;
|
2011-11-28 22:06:53 +00:00
|
|
|
snippet_data.hook = COGL_SNIPPET_HOOK_VERTEX_TRANSFORM;
|
|
|
|
snippet_data.chain_function = "cogl_real_vertex_transform";
|
|
|
|
snippet_data.final_name = "cogl_vertex_transform";
|
|
|
|
snippet_data.function_prefix = "cogl_vertex_transform";
|
|
|
|
snippet_data.source_buf = shader_state->header;
|
|
|
|
_cogl_pipeline_snippet_generate_code (&snippet_data);
|
|
|
|
|
2012-11-08 16:56:02 +00:00
|
|
|
/* Add hooks for the point size calculation part */
|
|
|
|
if (has_per_vertex_point_size)
|
|
|
|
{
|
|
|
|
memset (&snippet_data, 0, sizeof (snippet_data));
|
|
|
|
snippet_data.snippets = vertex_snippets;
|
|
|
|
snippet_data.hook = COGL_SNIPPET_HOOK_POINT_SIZE;
|
|
|
|
snippet_data.chain_function = "cogl_real_point_size_calculation";
|
|
|
|
snippet_data.final_name = "cogl_point_size_calculation";
|
|
|
|
snippet_data.function_prefix = "cogl_point_size_calculation";
|
|
|
|
snippet_data.source_buf = shader_state->header;
|
|
|
|
_cogl_pipeline_snippet_generate_code (&snippet_data);
|
|
|
|
}
|
|
|
|
|
2011-11-25 15:31:21 +00:00
|
|
|
/* Add all of the hooks for vertex processing */
|
|
|
|
memset (&snippet_data, 0, sizeof (snippet_data));
|
2011-11-29 14:21:07 +00:00
|
|
|
snippet_data.snippets = vertex_snippets;
|
2011-11-25 20:54:14 +00:00
|
|
|
snippet_data.hook = COGL_SNIPPET_HOOK_VERTEX;
|
2011-11-25 15:31:21 +00:00
|
|
|
snippet_data.chain_function = "cogl_generated_source";
|
2011-11-29 14:21:07 +00:00
|
|
|
snippet_data.final_name = "cogl_vertex_hook";
|
2011-11-25 15:31:21 +00:00
|
|
|
snippet_data.function_prefix = "cogl_vertex_hook";
|
|
|
|
snippet_data.source_buf = shader_state->source;
|
|
|
|
_cogl_pipeline_snippet_generate_code (&snippet_data);
|
2010-12-02 20:48:45 +00:00
|
|
|
|
2011-11-29 14:21:07 +00:00
|
|
|
g_string_append (shader_state->source,
|
|
|
|
"void\n"
|
|
|
|
"main ()\n"
|
|
|
|
"{\n"
|
|
|
|
" cogl_vertex_hook ();\n");
|
|
|
|
|
|
|
|
/* If there are any snippets then we can't rely on the
|
|
|
|
projection matrix to flip the rendering for offscreen buffers
|
|
|
|
so we'll need to flip it using an extra statement and a
|
|
|
|
uniform */
|
|
|
|
if (_cogl_pipeline_has_vertex_snippets (pipeline))
|
|
|
|
{
|
|
|
|
g_string_append (shader_state->header,
|
|
|
|
"uniform vec4 _cogl_flip_vector;\n");
|
|
|
|
g_string_append (shader_state->source,
|
|
|
|
" cogl_position_out *= _cogl_flip_vector;\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
g_string_append (shader_state->source,
|
|
|
|
"}\n");
|
|
|
|
|
2011-07-06 20:51:00 +00:00
|
|
|
GE_RET( shader, ctx, glCreateShader (GL_VERTEX_SHADER) );
|
2010-12-02 20:48:45 +00:00
|
|
|
|
2011-06-30 12:39:48 +00:00
|
|
|
lengths[0] = shader_state->header->len;
|
|
|
|
source_strings[0] = shader_state->header->str;
|
|
|
|
lengths[1] = shader_state->source->len;
|
|
|
|
source_strings[1] = shader_state->source->str;
|
2010-12-02 20:48:45 +00:00
|
|
|
|
2013-01-07 19:16:11 +00:00
|
|
|
_cogl_glsl_shader_set_source_with_boilerplate (ctx,
|
2012-09-23 12:32:36 +00:00
|
|
|
NULL,
|
2013-01-07 19:16:11 +00:00
|
|
|
shader, GL_VERTEX_SHADER,
|
2013-01-19 16:00:33 +00:00
|
|
|
pipeline,
|
2013-01-07 19:16:11 +00:00
|
|
|
2, /* count */
|
|
|
|
source_strings, lengths);
|
2010-12-02 20:48:45 +00:00
|
|
|
|
2011-07-06 20:51:00 +00:00
|
|
|
GE( ctx, glCompileShader (shader) );
|
|
|
|
GE( ctx, glGetShaderiv (shader, GL_COMPILE_STATUS, &compile_status) );
|
2010-12-02 20:48:45 +00:00
|
|
|
|
|
|
|
if (!compile_status)
|
|
|
|
{
|
|
|
|
GLint len = 0;
|
|
|
|
char *shader_log;
|
|
|
|
|
2011-07-06 20:51:00 +00:00
|
|
|
GE( ctx, glGetShaderiv (shader, GL_INFO_LOG_LENGTH, &len) );
|
2010-12-02 20:48:45 +00:00
|
|
|
shader_log = g_alloca (len);
|
2011-07-06 20:51:00 +00:00
|
|
|
GE( ctx, glGetShaderInfoLog (shader, len, &len, shader_log) );
|
2010-12-02 20:48:45 +00:00
|
|
|
g_warning ("Shader compilation failed:\n%s", shader_log);
|
|
|
|
}
|
|
|
|
|
2011-06-30 12:39:48 +00:00
|
|
|
shader_state->header = NULL;
|
|
|
|
shader_state->source = NULL;
|
|
|
|
shader_state->gl_shader = shader;
|
2010-12-02 20:48:45 +00:00
|
|
|
}
|
|
|
|
|
2012-11-08 16:56:02 +00:00
|
|
|
#ifdef HAVE_COGL_GL
|
2012-09-26 19:32:36 +00:00
|
|
|
if ((ctx->private_feature_flags &
|
|
|
|
COGL_PRIVATE_FEATURE_BUILTIN_POINT_SIZE_UNIFORM) &&
|
2012-02-28 14:10:43 +00:00
|
|
|
(pipelines_difference & COGL_PIPELINE_STATE_POINT_SIZE))
|
|
|
|
{
|
|
|
|
CoglPipeline *authority =
|
|
|
|
_cogl_pipeline_get_authority (pipeline, COGL_PIPELINE_STATE_POINT_SIZE);
|
|
|
|
|
2013-06-20 12:25:49 +00:00
|
|
|
if (authority->big_state->point_size > 0.0f)
|
|
|
|
GE( ctx, glPointSize (authority->big_state->point_size) );
|
2012-02-28 14:10:43 +00:00
|
|
|
}
|
2012-11-08 16:56:02 +00:00
|
|
|
#endif /* HAVE_COGL_GL */
|
2012-02-28 14:10:43 +00:00
|
|
|
|
2010-12-02 20:48:45 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_cogl_pipeline_vertend_glsl_pre_change_notify (CoglPipeline *pipeline,
|
|
|
|
CoglPipelineState change,
|
|
|
|
const CoglColor *new_color)
|
|
|
|
{
|
2013-06-20 12:25:49 +00:00
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
|
|
|
if ((change & _cogl_pipeline_get_state_for_vertex_codegen (ctx)))
|
2011-06-30 12:39:48 +00:00
|
|
|
dirty_shader_state (pipeline);
|
2010-12-02 20:48:45 +00: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
|
|
|
|
_cogl_pipeline_vertend_glsl_layer_pre_change_notify (
|
|
|
|
CoglPipeline *owner,
|
|
|
|
CoglPipelineLayer *layer,
|
|
|
|
CoglPipelineLayerState change)
|
|
|
|
{
|
2011-06-30 12:39:48 +00:00
|
|
|
CoglPipelineShaderState *shader_state;
|
2010-12-02 20:48:45 +00:00
|
|
|
|
2011-06-30 12:39:48 +00:00
|
|
|
shader_state = get_shader_state (owner);
|
|
|
|
if (!shader_state)
|
2010-12-02 20:48:45 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if ((change & COGL_PIPELINE_LAYER_STATE_AFFECTS_VERTEX_CODEGEN))
|
|
|
|
{
|
2011-06-30 12:39:48 +00:00
|
|
|
dirty_shader_state (owner);
|
2010-12-02 20:48:45 +00: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. */
|
|
|
|
}
|
|
|
|
|
|
|
|
const CoglPipelineVertend _cogl_pipeline_glsl_vertend =
|
|
|
|
{
|
|
|
|
_cogl_pipeline_vertend_glsl_start,
|
|
|
|
_cogl_pipeline_vertend_glsl_add_layer,
|
|
|
|
_cogl_pipeline_vertend_glsl_end,
|
|
|
|
_cogl_pipeline_vertend_glsl_pre_change_notify,
|
|
|
|
_cogl_pipeline_vertend_glsl_layer_pre_change_notify
|
|
|
|
};
|
|
|
|
|
2013-06-20 12:25:49 +00:00
|
|
|
UNIT_TEST (check_point_size_shader,
|
|
|
|
0 /* no requirements */,
|
|
|
|
0 /* no failure cases */)
|
|
|
|
{
|
|
|
|
CoglPipeline *pipelines[4];
|
|
|
|
CoglPipelineShaderState *shader_states[G_N_ELEMENTS (pipelines)];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Default pipeline with zero point size */
|
|
|
|
pipelines[0] = cogl_pipeline_new (test_ctx);
|
|
|
|
|
|
|
|
/* Point size 1 */
|
|
|
|
pipelines[1] = cogl_pipeline_new (test_ctx);
|
|
|
|
cogl_pipeline_set_point_size (pipelines[1], 1.0f);
|
|
|
|
|
|
|
|
/* Point size 2 */
|
|
|
|
pipelines[2] = cogl_pipeline_new (test_ctx);
|
|
|
|
cogl_pipeline_set_point_size (pipelines[2], 2.0f);
|
|
|
|
|
|
|
|
/* Same as the first pipeline, but reached by restoring the old
|
|
|
|
* state from a copy */
|
|
|
|
pipelines[3] = cogl_pipeline_copy (pipelines[1]);
|
|
|
|
cogl_pipeline_set_point_size (pipelines[3], 0.0f);
|
|
|
|
|
|
|
|
/* Draw something with all of the pipelines to make sure their state
|
|
|
|
* is flushed */
|
|
|
|
for (i = 0; i < G_N_ELEMENTS (pipelines); i++)
|
|
|
|
cogl_framebuffer_draw_rectangle (test_fb,
|
|
|
|
pipelines[i],
|
|
|
|
0.0f, 0.0f,
|
|
|
|
10.0f, 10.0f);
|
|
|
|
cogl_framebuffer_finish (test_fb);
|
|
|
|
|
|
|
|
/* Get all of the shader states. These might be NULL if the driver
|
|
|
|
* is not using GLSL */
|
|
|
|
for (i = 0; i < G_N_ELEMENTS (pipelines); i++)
|
|
|
|
shader_states[i] = get_shader_state (pipelines[i]);
|
|
|
|
|
|
|
|
/* If the first two pipelines are using GLSL then they should have
|
|
|
|
* the same shader unless there is no builtin uniform for the point
|
|
|
|
* size */
|
|
|
|
if (shader_states[0])
|
|
|
|
{
|
|
|
|
if ((test_ctx->private_feature_flags &
|
|
|
|
COGL_PRIVATE_FEATURE_BUILTIN_POINT_SIZE_UNIFORM))
|
|
|
|
g_assert (shader_states[0] == shader_states[1]);
|
|
|
|
else
|
|
|
|
g_assert (shader_states[0] != shader_states[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The second and third pipelines should always have the same shader
|
|
|
|
* state because only toggling between zero and non-zero should
|
|
|
|
* change the shader */
|
|
|
|
g_assert (shader_states[1] == shader_states[2]);
|
|
|
|
|
|
|
|
/* The fourth pipeline should be exactly the same as the first */
|
|
|
|
g_assert (shader_states[0] == shader_states[3]);
|
|
|
|
}
|
|
|
|
|
2010-12-02 20:48:45 +00:00
|
|
|
#endif /* COGL_PIPELINE_VERTEND_GLSL */
|