2009-03-30 12:07:31 -04:00
|
|
|
/*
|
|
|
|
* Cogl
|
|
|
|
*
|
2009-04-27 10:48:12 -04:00
|
|
|
* An object oriented GL/GLES Abstraction/Utility Layer
|
2009-03-30 12:07:31 -04:00
|
|
|
*
|
2009-04-27 10:48:12 -04:00
|
|
|
* Copyright (C) 2007,2008,2009 Intel Corporation.
|
2009-03-30 12:07:31 -04: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, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "cogl.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
Intial Re-layout of the Cogl source code and introduction of a Cogl Winsys
As part of an incremental process to have Cogl be a standalone project we
want to re-consider how we organise the Cogl source code.
Currently this is the structure I'm aiming for:
cogl/
cogl/
<put common source here>
winsys/
cogl-glx.c
cogl-wgl.c
driver/
gl/
gles/
os/ ?
utils/
cogl-fixed
cogl-matrix-stack?
cogl-journal?
cogl-primitives?
pango/
The new winsys component is a starting point for migrating window system
code (i.e. x11,glx,wgl,osx,egl etc) from Clutter to Cogl.
The utils/ and pango/ directories aren't added by this commit, but they are
noted because I plan to add them soon.
Overview of the planned structure:
* The winsys/ API is the API that binds OpenGL to a specific window system,
be that X11 or win32 etc. Example are glx, wgl and egl. Much of the logic
under clutter/{glx,osx,win32 etc} should migrate here.
* Note there is also the idea of a winsys-base that may represent a window
system for which there are multiple winsys APIs. An example of this is
x11, since glx and egl may both be used with x11. (currently only Clutter
has the idea of a winsys-base)
* The driver/ represents a specific varient of OpenGL. Currently we have "gl"
representing OpenGL 1.4-2.1 (mostly fixed function) and "gles" representing
GLES 1.1 (fixed funciton) and 2.0 (fully shader based)
* Everything under cogl/ should fundamentally be supporting access to the
GPU. Essentially Cogl's most basic requirement is to provide a nice GPU
Graphics API and drawing a line between this and the utility functionality
we add to support Clutter should help keep this lean and maintainable.
* Code under utils/ as suggested builds on cogl/ adding more convenient
APIs or mechanism to optimize special cases. Broadly speaking you can
compare cogl/ to OpenGL and utils/ to GLU.
* clutter/pango will be moved to clutter/cogl/pango
How some of the internal configure.ac/pkg-config terminology has changed:
backendextra -> CLUTTER_WINSYS_BASE # e.g. "x11"
backendextralib -> CLUTTER_WINSYS_BASE_LIB # e.g. "x11/libclutter-x11.la"
clutterbackend -> {CLUTTER,COGL}_WINSYS # e.g. "glx"
CLUTTER_FLAVOUR -> {CLUTTER,COGL}_WINSYS
clutterbackendlib -> CLUTTER_WINSYS_LIB
CLUTTER_COGL -> COGL_DRIVER # e.g. "gl"
Note: The CLUTTER_FLAVOUR and CLUTTER_COGL defines are kept for apps
As the first thing to take advantage of the new winsys component in Cogl;
cogl_get_proc_address() has been moved from cogl/{gl,gles}/cogl.c into
cogl/common/cogl.c and this common implementation first trys
_cogl_winsys_get_proc_address() but if that fails then it falls back to
gmodule.
2009-07-27 21:02:02 -04:00
|
|
|
#include <gmodule.h>
|
2009-03-30 12:07:31 -04:00
|
|
|
|
2009-05-19 09:44:29 -04:00
|
|
|
#include "cogl-debug.h"
|
2009-03-30 12:07:31 -04:00
|
|
|
#include "cogl-internal.h"
|
|
|
|
#include "cogl-util.h"
|
|
|
|
#include "cogl-context.h"
|
2009-05-23 12:42:10 -04:00
|
|
|
#include "cogl-material-private.h"
|
Intial Re-layout of the Cogl source code and introduction of a Cogl Winsys
As part of an incremental process to have Cogl be a standalone project we
want to re-consider how we organise the Cogl source code.
Currently this is the structure I'm aiming for:
cogl/
cogl/
<put common source here>
winsys/
cogl-glx.c
cogl-wgl.c
driver/
gl/
gles/
os/ ?
utils/
cogl-fixed
cogl-matrix-stack?
cogl-journal?
cogl-primitives?
pango/
The new winsys component is a starting point for migrating window system
code (i.e. x11,glx,wgl,osx,egl etc) from Clutter to Cogl.
The utils/ and pango/ directories aren't added by this commit, but they are
noted because I plan to add them soon.
Overview of the planned structure:
* The winsys/ API is the API that binds OpenGL to a specific window system,
be that X11 or win32 etc. Example are glx, wgl and egl. Much of the logic
under clutter/{glx,osx,win32 etc} should migrate here.
* Note there is also the idea of a winsys-base that may represent a window
system for which there are multiple winsys APIs. An example of this is
x11, since glx and egl may both be used with x11. (currently only Clutter
has the idea of a winsys-base)
* The driver/ represents a specific varient of OpenGL. Currently we have "gl"
representing OpenGL 1.4-2.1 (mostly fixed function) and "gles" representing
GLES 1.1 (fixed funciton) and 2.0 (fully shader based)
* Everything under cogl/ should fundamentally be supporting access to the
GPU. Essentially Cogl's most basic requirement is to provide a nice GPU
Graphics API and drawing a line between this and the utility functionality
we add to support Clutter should help keep this lean and maintainable.
* Code under utils/ as suggested builds on cogl/ adding more convenient
APIs or mechanism to optimize special cases. Broadly speaking you can
compare cogl/ to OpenGL and utils/ to GLU.
* clutter/pango will be moved to clutter/cogl/pango
How some of the internal configure.ac/pkg-config terminology has changed:
backendextra -> CLUTTER_WINSYS_BASE # e.g. "x11"
backendextralib -> CLUTTER_WINSYS_BASE_LIB # e.g. "x11/libclutter-x11.la"
clutterbackend -> {CLUTTER,COGL}_WINSYS # e.g. "glx"
CLUTTER_FLAVOUR -> {CLUTTER,COGL}_WINSYS
clutterbackendlib -> CLUTTER_WINSYS_LIB
CLUTTER_COGL -> COGL_DRIVER # e.g. "gl"
Note: The CLUTTER_FLAVOUR and CLUTTER_COGL defines are kept for apps
As the first thing to take advantage of the new winsys component in Cogl;
cogl_get_proc_address() has been moved from cogl/{gl,gles}/cogl.c into
cogl/common/cogl.c and this common implementation first trys
_cogl_winsys_get_proc_address() but if that fails then it falls back to
gmodule.
2009-07-27 21:02:02 -04:00
|
|
|
#include "cogl-winsys.h"
|
2009-09-25 09:34:34 -04:00
|
|
|
#include "cogl-draw-buffer-private.h"
|
2009-10-26 07:01:33 -04:00
|
|
|
#include "cogl-matrix-private.h"
|
2009-03-30 12:07:31 -04:00
|
|
|
|
|
|
|
#if defined (HAVE_COGL_GLES2) || defined (HAVE_COGL_GLES)
|
|
|
|
#include "cogl-gles2-wrapper.h"
|
|
|
|
#endif
|
|
|
|
|
2009-07-01 10:27:11 -04:00
|
|
|
#ifdef HAVE_COGL_GL
|
2009-07-27 20:34:33 -04:00
|
|
|
#define glClientActiveTexture ctx->drv.pf_glClientActiveTexture
|
2009-07-01 10:27:11 -04:00
|
|
|
#endif
|
|
|
|
|
2009-05-19 09:44:29 -04:00
|
|
|
#ifdef COGL_GL_DEBUG
|
2009-03-30 12:07:31 -04:00
|
|
|
/* GL error to string conversion */
|
2009-05-18 14:38:03 -04:00
|
|
|
static const struct {
|
|
|
|
GLuint error_code;
|
|
|
|
const gchar *error_string;
|
|
|
|
} gl_errors[] = {
|
|
|
|
{ GL_NO_ERROR, "No error" },
|
|
|
|
{ GL_INVALID_ENUM, "Invalid enumeration value" },
|
|
|
|
{ GL_INVALID_VALUE, "Invalid value" },
|
|
|
|
{ GL_INVALID_OPERATION, "Invalid operation" },
|
|
|
|
{ GL_STACK_OVERFLOW, "Stack overflow" },
|
|
|
|
{ GL_STACK_UNDERFLOW, "Stack underflow" },
|
|
|
|
{ GL_OUT_OF_MEMORY, "Out of memory" },
|
2009-03-30 12:07:31 -04:00
|
|
|
|
|
|
|
#ifdef GL_INVALID_FRAMEBUFFER_OPERATION_EXT
|
2009-05-18 14:38:03 -04:00
|
|
|
{ GL_INVALID_FRAMEBUFFER_OPERATION_EXT, "Invalid framebuffer operation" }
|
2009-03-30 12:07:31 -04:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2009-05-18 14:38:03 -04:00
|
|
|
static const guint n_gl_errors = G_N_ELEMENTS (gl_errors);
|
|
|
|
|
|
|
|
const gchar *
|
|
|
|
cogl_gl_error_to_string (GLenum error_code)
|
2009-03-30 12:07:31 -04:00
|
|
|
{
|
2009-05-18 14:38:03 -04:00
|
|
|
gint i;
|
|
|
|
|
|
|
|
for (i = 0; i < n_gl_errors; i++)
|
|
|
|
{
|
|
|
|
if (gl_errors[i].error_code == error_code)
|
|
|
|
return gl_errors[i].error_string;
|
|
|
|
}
|
|
|
|
|
2009-05-19 09:44:29 -04:00
|
|
|
return "Unknown GL error";
|
2009-03-30 12:07:31 -04:00
|
|
|
}
|
2009-05-19 09:44:29 -04:00
|
|
|
#endif /* COGL_GL_DEBUG */
|
2009-03-30 12:07:31 -04:00
|
|
|
|
Intial Re-layout of the Cogl source code and introduction of a Cogl Winsys
As part of an incremental process to have Cogl be a standalone project we
want to re-consider how we organise the Cogl source code.
Currently this is the structure I'm aiming for:
cogl/
cogl/
<put common source here>
winsys/
cogl-glx.c
cogl-wgl.c
driver/
gl/
gles/
os/ ?
utils/
cogl-fixed
cogl-matrix-stack?
cogl-journal?
cogl-primitives?
pango/
The new winsys component is a starting point for migrating window system
code (i.e. x11,glx,wgl,osx,egl etc) from Clutter to Cogl.
The utils/ and pango/ directories aren't added by this commit, but they are
noted because I plan to add them soon.
Overview of the planned structure:
* The winsys/ API is the API that binds OpenGL to a specific window system,
be that X11 or win32 etc. Example are glx, wgl and egl. Much of the logic
under clutter/{glx,osx,win32 etc} should migrate here.
* Note there is also the idea of a winsys-base that may represent a window
system for which there are multiple winsys APIs. An example of this is
x11, since glx and egl may both be used with x11. (currently only Clutter
has the idea of a winsys-base)
* The driver/ represents a specific varient of OpenGL. Currently we have "gl"
representing OpenGL 1.4-2.1 (mostly fixed function) and "gles" representing
GLES 1.1 (fixed funciton) and 2.0 (fully shader based)
* Everything under cogl/ should fundamentally be supporting access to the
GPU. Essentially Cogl's most basic requirement is to provide a nice GPU
Graphics API and drawing a line between this and the utility functionality
we add to support Clutter should help keep this lean and maintainable.
* Code under utils/ as suggested builds on cogl/ adding more convenient
APIs or mechanism to optimize special cases. Broadly speaking you can
compare cogl/ to OpenGL and utils/ to GLU.
* clutter/pango will be moved to clutter/cogl/pango
How some of the internal configure.ac/pkg-config terminology has changed:
backendextra -> CLUTTER_WINSYS_BASE # e.g. "x11"
backendextralib -> CLUTTER_WINSYS_BASE_LIB # e.g. "x11/libclutter-x11.la"
clutterbackend -> {CLUTTER,COGL}_WINSYS # e.g. "glx"
CLUTTER_FLAVOUR -> {CLUTTER,COGL}_WINSYS
clutterbackendlib -> CLUTTER_WINSYS_LIB
CLUTTER_COGL -> COGL_DRIVER # e.g. "gl"
Note: The CLUTTER_FLAVOUR and CLUTTER_COGL defines are kept for apps
As the first thing to take advantage of the new winsys component in Cogl;
cogl_get_proc_address() has been moved from cogl/{gl,gles}/cogl.c into
cogl/common/cogl.c and this common implementation first trys
_cogl_winsys_get_proc_address() but if that fails then it falls back to
gmodule.
2009-07-27 21:02:02 -04:00
|
|
|
CoglFuncPtr
|
|
|
|
cogl_get_proc_address (const gchar* name)
|
|
|
|
{
|
|
|
|
void *address;
|
|
|
|
static GModule *module = NULL;
|
|
|
|
|
|
|
|
address = _cogl_winsys_get_proc_address (name);
|
|
|
|
if (address)
|
|
|
|
return address;
|
|
|
|
|
|
|
|
/* this should find the right function if the program is linked against a
|
|
|
|
* library providing it */
|
2009-11-06 09:04:36 -05:00
|
|
|
if (G_UNLIKELY (module == NULL))
|
|
|
|
module = g_module_open (NULL, 0);
|
Intial Re-layout of the Cogl source code and introduction of a Cogl Winsys
As part of an incremental process to have Cogl be a standalone project we
want to re-consider how we organise the Cogl source code.
Currently this is the structure I'm aiming for:
cogl/
cogl/
<put common source here>
winsys/
cogl-glx.c
cogl-wgl.c
driver/
gl/
gles/
os/ ?
utils/
cogl-fixed
cogl-matrix-stack?
cogl-journal?
cogl-primitives?
pango/
The new winsys component is a starting point for migrating window system
code (i.e. x11,glx,wgl,osx,egl etc) from Clutter to Cogl.
The utils/ and pango/ directories aren't added by this commit, but they are
noted because I plan to add them soon.
Overview of the planned structure:
* The winsys/ API is the API that binds OpenGL to a specific window system,
be that X11 or win32 etc. Example are glx, wgl and egl. Much of the logic
under clutter/{glx,osx,win32 etc} should migrate here.
* Note there is also the idea of a winsys-base that may represent a window
system for which there are multiple winsys APIs. An example of this is
x11, since glx and egl may both be used with x11. (currently only Clutter
has the idea of a winsys-base)
* The driver/ represents a specific varient of OpenGL. Currently we have "gl"
representing OpenGL 1.4-2.1 (mostly fixed function) and "gles" representing
GLES 1.1 (fixed funciton) and 2.0 (fully shader based)
* Everything under cogl/ should fundamentally be supporting access to the
GPU. Essentially Cogl's most basic requirement is to provide a nice GPU
Graphics API and drawing a line between this and the utility functionality
we add to support Clutter should help keep this lean and maintainable.
* Code under utils/ as suggested builds on cogl/ adding more convenient
APIs or mechanism to optimize special cases. Broadly speaking you can
compare cogl/ to OpenGL and utils/ to GLU.
* clutter/pango will be moved to clutter/cogl/pango
How some of the internal configure.ac/pkg-config terminology has changed:
backendextra -> CLUTTER_WINSYS_BASE # e.g. "x11"
backendextralib -> CLUTTER_WINSYS_BASE_LIB # e.g. "x11/libclutter-x11.la"
clutterbackend -> {CLUTTER,COGL}_WINSYS # e.g. "glx"
CLUTTER_FLAVOUR -> {CLUTTER,COGL}_WINSYS
clutterbackendlib -> CLUTTER_WINSYS_LIB
CLUTTER_COGL -> COGL_DRIVER # e.g. "gl"
Note: The CLUTTER_FLAVOUR and CLUTTER_COGL defines are kept for apps
As the first thing to take advantage of the new winsys component in Cogl;
cogl_get_proc_address() has been moved from cogl/{gl,gles}/cogl.c into
cogl/common/cogl.c and this common implementation first trys
_cogl_winsys_get_proc_address() but if that fails then it falls back to
gmodule.
2009-07-27 21:02:02 -04:00
|
|
|
|
|
|
|
if (module)
|
|
|
|
{
|
|
|
|
gpointer symbol;
|
|
|
|
|
|
|
|
if (g_module_symbol (module, name, &symbol))
|
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-03-30 12:07:31 -04:00
|
|
|
void
|
2009-04-24 13:09:52 -04:00
|
|
|
cogl_clear (const CoglColor *color, gulong buffers)
|
2009-03-30 12:07:31 -04:00
|
|
|
{
|
2009-04-24 13:09:52 -04:00
|
|
|
GLbitfield gl_buffers = 0;
|
|
|
|
|
2009-05-19 09:44:29 -04:00
|
|
|
COGL_NOTE (DRAW, "Clear begin");
|
2009-03-30 12:07:31 -04:00
|
|
|
|
2009-09-25 09:34:34 -04:00
|
|
|
_cogl_journal_flush ();
|
|
|
|
|
|
|
|
/* NB: _cogl_draw_buffer_flush_state may disrupt various state (such
|
|
|
|
* as the material state) when flushing the clip stack, so should
|
|
|
|
* always be done first when preparing to draw. */
|
|
|
|
_cogl_draw_buffer_flush_state (_cogl_get_draw_buffer (), 0);
|
2009-05-08 11:32:01 -04:00
|
|
|
|
2009-04-24 13:09:52 -04:00
|
|
|
if (buffers & COGL_BUFFER_BIT_COLOR)
|
|
|
|
{
|
|
|
|
GE( glClearColor (cogl_color_get_red_float (color),
|
|
|
|
cogl_color_get_green_float (color),
|
|
|
|
cogl_color_get_blue_float (color),
|
2009-08-11 08:49:12 -04:00
|
|
|
cogl_color_get_alpha_float (color)) );
|
2009-04-24 13:09:52 -04:00
|
|
|
gl_buffers |= GL_COLOR_BUFFER_BIT;
|
|
|
|
}
|
2009-05-19 09:44:29 -04:00
|
|
|
|
2009-04-24 13:09:52 -04:00
|
|
|
if (buffers & COGL_BUFFER_BIT_DEPTH)
|
|
|
|
gl_buffers |= GL_DEPTH_BUFFER_BIT;
|
2009-05-19 09:44:29 -04:00
|
|
|
|
2009-04-24 13:09:52 -04:00
|
|
|
if (buffers & COGL_BUFFER_BIT_STENCIL)
|
|
|
|
gl_buffers |= GL_STENCIL_BUFFER_BIT;
|
|
|
|
|
|
|
|
if (!gl_buffers)
|
|
|
|
{
|
|
|
|
static gboolean shown = FALSE;
|
2009-05-19 09:44:29 -04:00
|
|
|
|
2009-04-24 13:09:52 -04:00
|
|
|
if (!shown)
|
2009-05-19 09:44:29 -04:00
|
|
|
{
|
|
|
|
g_warning ("You should specify at least one auxiliary buffer "
|
|
|
|
"when calling cogl_clear");
|
|
|
|
}
|
|
|
|
|
2009-04-24 13:09:52 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
glClear (gl_buffers);
|
2009-05-19 09:44:29 -04:00
|
|
|
|
|
|
|
COGL_NOTE (DRAW, "Clear end");
|
2009-03-30 12:07:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline gboolean
|
|
|
|
cogl_toggle_flag (CoglContext *ctx,
|
|
|
|
gulong new_flags,
|
|
|
|
gulong flag,
|
|
|
|
GLenum gl_flag)
|
|
|
|
{
|
|
|
|
/* Toggles and caches a single enable flag on or off
|
|
|
|
* by comparing to current state
|
|
|
|
*/
|
|
|
|
if (new_flags & flag)
|
|
|
|
{
|
|
|
|
if (!(ctx->enable_flags & flag))
|
|
|
|
{
|
|
|
|
GE( glEnable (gl_flag) );
|
|
|
|
ctx->enable_flags |= flag;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ctx->enable_flags & flag)
|
|
|
|
{
|
|
|
|
GE( glDisable (gl_flag) );
|
|
|
|
ctx->enable_flags &= ~flag;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline gboolean
|
|
|
|
cogl_toggle_client_flag (CoglContext *ctx,
|
|
|
|
gulong new_flags,
|
|
|
|
gulong flag,
|
|
|
|
GLenum gl_flag)
|
|
|
|
{
|
|
|
|
/* Toggles and caches a single client-side enable flag
|
|
|
|
* on or off by comparing to current state
|
|
|
|
*/
|
|
|
|
if (new_flags & flag)
|
|
|
|
{
|
|
|
|
if (!(ctx->enable_flags & flag))
|
|
|
|
{
|
|
|
|
GE( glEnableClientState (gl_flag) );
|
|
|
|
ctx->enable_flags |= flag;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ctx->enable_flags & flag)
|
|
|
|
{
|
|
|
|
GE( glDisableClientState (gl_flag) );
|
|
|
|
ctx->enable_flags &= ~flag;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cogl_enable (gulong flags)
|
|
|
|
{
|
|
|
|
/* This function essentially caches glEnable state() in the
|
|
|
|
* hope of lessening number GL traffic.
|
|
|
|
*/
|
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
|
|
|
cogl_toggle_flag (ctx, flags,
|
|
|
|
COGL_ENABLE_BLEND,
|
|
|
|
GL_BLEND);
|
|
|
|
|
|
|
|
cogl_toggle_flag (ctx, flags,
|
|
|
|
COGL_ENABLE_BACKFACE_CULLING,
|
|
|
|
GL_CULL_FACE);
|
|
|
|
|
|
|
|
cogl_toggle_client_flag (ctx, flags,
|
|
|
|
COGL_ENABLE_VERTEX_ARRAY,
|
|
|
|
GL_VERTEX_ARRAY);
|
|
|
|
|
|
|
|
cogl_toggle_client_flag (ctx, flags,
|
|
|
|
COGL_ENABLE_COLOR_ARRAY,
|
|
|
|
GL_COLOR_ARRAY);
|
|
|
|
}
|
|
|
|
|
|
|
|
gulong
|
|
|
|
cogl_get_enable ()
|
|
|
|
{
|
|
|
|
_COGL_GET_CONTEXT (ctx, 0);
|
|
|
|
|
|
|
|
return ctx->enable_flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-05-26 11:55:11 -04:00
|
|
|
cogl_set_depth_test_enabled (gboolean setting)
|
2009-03-30 12:07:31 -04:00
|
|
|
{
|
[cogl] Improving Cogl journal to minimize driver overheads + GPU state changes
Previously the journal was always flushed at the end of
_cogl_rectangles_with_multitexture_coords, (i.e. the end of any
cogl_rectangle* calls) but now we have broadened the potential for batching
geometry. In ideal circumstances we will only flush once per scene.
In summary the journal works like this:
When you use any of the cogl_rectangle* APIs then nothing is emitted to the
GPU at this point, we just log one or more quads into the journal. A
journal entry consists of the quad coordinates, an associated material
reference, and a modelview matrix. Ideally the journal only gets flushed
once at the end of a scene, but in fact there are things to consider that
may cause unwanted flushing, including:
- modifying materials mid-scene
This is because each quad in the journal has an associated material
reference (i.e. not copy), so if you try and modify a material that is
already referenced in the journal we force a flush first)
NOTE: For now this means you should avoid using cogl_set_source_color()
since that currently uses a single shared material. Later we
should change it to use a pool of materials that is recycled
when the journal is flushed.
- modifying any state that isn't currently logged, such as depth, fog and
backface culling enables.
The first thing that happens when flushing, is to upload all the vertex data
associated with the journal into a single VBO.
We then go through a process of splitting up the journal into batches that
have compatible state so they can be emitted to the GPU together. This is
currently broken up into 3 levels so we can stagger the state changes:
1) we break the journal up according to changes in the number of material layers
associated with logged quads. The number of layers in a material determines
the stride of the associated vertices, so we have to update our vertex
array offsets at this level. (i.e. calling gl{Vertex,Color},Pointer etc)
2) we further split batches up according to material compatability. (e.g.
materials with different textures) We flush material state at this level.
3) Finally we split batches up according to modelview changes. At this level
we update the modelview matrix and actually emit the actual draw command.
This commit is largely about putting the initial design in-place; this will be
followed by other changes that take advantage of the extended batching.
2009-06-17 13:46:42 -04:00
|
|
|
/* Currently the journal can't track changes to depth state... */
|
|
|
|
_cogl_journal_flush ();
|
|
|
|
|
2009-03-30 12:07:31 -04:00
|
|
|
if (setting)
|
|
|
|
{
|
|
|
|
glEnable (GL_DEPTH_TEST);
|
|
|
|
glDepthFunc (GL_LEQUAL);
|
|
|
|
}
|
|
|
|
else
|
2009-05-26 11:55:11 -04:00
|
|
|
glDisable (GL_DEPTH_TEST);
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
cogl_get_depth_test_enabled (void)
|
|
|
|
{
|
|
|
|
return glIsEnabled (GL_DEPTH_TEST) == GL_TRUE ? TRUE : FALSE;
|
2009-03-30 12:07:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-05-26 11:55:11 -04:00
|
|
|
cogl_set_backface_culling_enabled (gboolean setting)
|
2009-03-30 12:07:31 -04:00
|
|
|
{
|
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
2009-10-22 14:09:20 -04:00
|
|
|
if (ctx->enable_backface_culling == setting)
|
|
|
|
return;
|
|
|
|
|
[cogl] Improving Cogl journal to minimize driver overheads + GPU state changes
Previously the journal was always flushed at the end of
_cogl_rectangles_with_multitexture_coords, (i.e. the end of any
cogl_rectangle* calls) but now we have broadened the potential for batching
geometry. In ideal circumstances we will only flush once per scene.
In summary the journal works like this:
When you use any of the cogl_rectangle* APIs then nothing is emitted to the
GPU at this point, we just log one or more quads into the journal. A
journal entry consists of the quad coordinates, an associated material
reference, and a modelview matrix. Ideally the journal only gets flushed
once at the end of a scene, but in fact there are things to consider that
may cause unwanted flushing, including:
- modifying materials mid-scene
This is because each quad in the journal has an associated material
reference (i.e. not copy), so if you try and modify a material that is
already referenced in the journal we force a flush first)
NOTE: For now this means you should avoid using cogl_set_source_color()
since that currently uses a single shared material. Later we
should change it to use a pool of materials that is recycled
when the journal is flushed.
- modifying any state that isn't currently logged, such as depth, fog and
backface culling enables.
The first thing that happens when flushing, is to upload all the vertex data
associated with the journal into a single VBO.
We then go through a process of splitting up the journal into batches that
have compatible state so they can be emitted to the GPU together. This is
currently broken up into 3 levels so we can stagger the state changes:
1) we break the journal up according to changes in the number of material layers
associated with logged quads. The number of layers in a material determines
the stride of the associated vertices, so we have to update our vertex
array offsets at this level. (i.e. calling gl{Vertex,Color},Pointer etc)
2) we further split batches up according to material compatability. (e.g.
materials with different textures) We flush material state at this level.
3) Finally we split batches up according to modelview changes. At this level
we update the modelview matrix and actually emit the actual draw command.
This commit is largely about putting the initial design in-place; this will be
followed by other changes that take advantage of the extended batching.
2009-06-17 13:46:42 -04:00
|
|
|
/* Currently the journal can't track changes to backface culling state... */
|
|
|
|
_cogl_journal_flush ();
|
|
|
|
|
2009-03-30 12:07:31 -04:00
|
|
|
ctx->enable_backface_culling = setting;
|
|
|
|
}
|
|
|
|
|
2009-05-26 11:55:11 -04:00
|
|
|
gboolean
|
|
|
|
cogl_get_backface_culling_enabled (void)
|
|
|
|
{
|
|
|
|
_COGL_GET_CONTEXT (ctx, FALSE);
|
|
|
|
|
|
|
|
return ctx->enable_backface_culling;
|
|
|
|
}
|
|
|
|
|
2009-10-22 14:01:52 -04:00
|
|
|
void
|
|
|
|
_cogl_flush_face_winding (void)
|
|
|
|
{
|
|
|
|
CoglFrontWinding winding;
|
|
|
|
|
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
|
|
|
/* The front face winding doesn't matter if we aren't performing any
|
|
|
|
* backface culling... */
|
|
|
|
if (!ctx->enable_backface_culling)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* NB: We use a clockwise face winding order when drawing offscreen because
|
|
|
|
* all offscreen rendering is done upside down resulting in reversed winding
|
|
|
|
* for all triangles.
|
|
|
|
*/
|
|
|
|
if (cogl_is_offscreen (_cogl_get_draw_buffer ()))
|
|
|
|
winding = COGL_FRONT_WINDING_CLOCKWISE;
|
|
|
|
else
|
|
|
|
winding = COGL_FRONT_WINDING_COUNTER_CLOCKWISE;
|
|
|
|
|
|
|
|
if (winding != ctx->flushed_front_winding)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (winding == COGL_FRONT_WINDING_CLOCKWISE)
|
|
|
|
GE (glFrontFace (GL_CW));
|
|
|
|
else
|
|
|
|
GE (glFrontFace (GL_CCW));
|
|
|
|
ctx->flushed_front_winding = winding;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-30 12:07:31 -04:00
|
|
|
void
|
|
|
|
cogl_set_source_color (const CoglColor *color)
|
|
|
|
{
|
2009-05-09 14:39:01 -04:00
|
|
|
CoglColor premultiplied;
|
|
|
|
|
2009-03-30 12:07:31 -04:00
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
|
|
|
/* In case cogl_set_source_texture was previously used... */
|
|
|
|
cogl_material_remove_layer (ctx->default_material, 0);
|
|
|
|
|
2009-05-09 14:39:01 -04:00
|
|
|
premultiplied = *color;
|
|
|
|
cogl_color_premultiply (&premultiplied);
|
|
|
|
cogl_material_set_color (ctx->default_material, &premultiplied);
|
|
|
|
|
2009-03-30 12:07:31 -04:00
|
|
|
cogl_set_source (ctx->default_material);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-11-03 08:26:58 -05:00
|
|
|
cogl_set_viewport (int x,
|
|
|
|
int y,
|
|
|
|
int width,
|
|
|
|
int height)
|
2009-03-30 12:07:31 -04:00
|
|
|
{
|
2009-09-25 09:34:34 -04:00
|
|
|
CoglHandle draw_buffer;
|
|
|
|
|
2009-07-01 07:57:30 -04:00
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
2009-09-25 09:34:34 -04:00
|
|
|
draw_buffer = _cogl_get_draw_buffer ();
|
2009-07-01 07:57:30 -04:00
|
|
|
|
2009-09-25 09:34:34 -04:00
|
|
|
_cogl_draw_buffer_set_viewport (draw_buffer,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
width,
|
|
|
|
height);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX: This should be deprecated, and we should expose a way to also
|
|
|
|
* specify an x and y viewport offset */
|
|
|
|
void
|
|
|
|
cogl_viewport (guint width,
|
|
|
|
guint height)
|
|
|
|
{
|
2009-11-03 08:26:58 -05:00
|
|
|
cogl_set_viewport (0, 0, width, height);
|
2009-03-30 12:07:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-05-28 08:03:19 -04:00
|
|
|
_cogl_setup_viewport (guint width,
|
|
|
|
guint height,
|
|
|
|
float fovy,
|
|
|
|
float aspect,
|
|
|
|
float z_near,
|
|
|
|
float z_far)
|
2009-03-30 12:07:31 -04:00
|
|
|
{
|
|
|
|
float z_camera;
|
|
|
|
CoglMatrix projection_matrix;
|
2009-09-25 09:34:34 -04:00
|
|
|
CoglMatrixStack *modelview_stack;
|
2009-03-30 12:07:31 -04:00
|
|
|
|
2009-10-13 18:09:42 -04:00
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
2009-11-03 08:26:58 -05:00
|
|
|
cogl_set_viewport (0, 0, width, height);
|
2009-03-30 12:07:31 -04:00
|
|
|
|
|
|
|
/* For Ortho projection.
|
2009-10-13 18:09:42 -04:00
|
|
|
* _cogl_matrix_stack_ortho (projection_stack, 0, width, 0, height, -1, 1);
|
2009-03-30 12:07:31 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
cogl_perspective (fovy, aspect, z_near, z_far);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In theory, we can compute the camera distance from screen as:
|
|
|
|
*
|
|
|
|
* 0.5 * tan (FOV)
|
|
|
|
*
|
|
|
|
* However, it's better to compute the z_camera from our projection
|
|
|
|
* matrix so that we get a 1:1 mapping at the screen distance. Consider
|
|
|
|
* the upper-left corner of the screen. It has object coordinates
|
|
|
|
* (0,0,0), so by the transform below, ends up with eye coordinate
|
|
|
|
*
|
|
|
|
* x_eye = x_object / width - 0.5 = - 0.5
|
|
|
|
* y_eye = (height - y_object) / width - 0.5 = 0.5
|
|
|
|
* z_eye = z_object / width - z_camera = - z_camera
|
|
|
|
*
|
|
|
|
* From cogl_perspective(), we know that the projection matrix has
|
|
|
|
* the form:
|
|
|
|
*
|
|
|
|
* (x, 0, 0, 0)
|
|
|
|
* (0, y, 0, 0)
|
|
|
|
* (0, 0, c, d)
|
|
|
|
* (0, 0, -1, 0)
|
|
|
|
*
|
|
|
|
* Applied to the above, we get clip coordinates of
|
|
|
|
*
|
|
|
|
* x_clip = x * (- 0.5)
|
|
|
|
* y_clip = y * 0.5
|
|
|
|
* w_clip = - 1 * (- z_camera) = z_camera
|
|
|
|
*
|
|
|
|
* Dividing through by w to get normalized device coordinates, we
|
|
|
|
* have, x_nd = x * 0.5 / z_camera, y_nd = - y * 0.5 / z_camera.
|
|
|
|
* The upper left corner of the screen has normalized device coordinates,
|
|
|
|
* (-1, 1), so to have the correct 1:1 mapping, we have to have:
|
|
|
|
*
|
|
|
|
* z_camera = 0.5 * x = 0.5 * y
|
|
|
|
*
|
|
|
|
* If x != y, then we have a non-uniform aspect ration, and a 1:1 mapping
|
|
|
|
* doesn't make sense.
|
|
|
|
*/
|
|
|
|
|
|
|
|
cogl_get_projection_matrix (&projection_matrix);
|
|
|
|
z_camera = 0.5 * projection_matrix.xx;
|
|
|
|
|
2009-09-25 09:34:34 -04:00
|
|
|
modelview_stack =
|
|
|
|
_cogl_draw_buffer_get_modelview_stack (_cogl_get_draw_buffer ());
|
|
|
|
_cogl_matrix_stack_load_identity (modelview_stack);
|
|
|
|
_cogl_matrix_stack_translate (modelview_stack, -0.5f, -0.5f, -z_camera);
|
|
|
|
_cogl_matrix_stack_scale (modelview_stack,
|
2009-10-13 18:09:42 -04:00
|
|
|
1.0f / width, -1.0f / height, 1.0f / width);
|
2009-09-25 09:34:34 -04:00
|
|
|
_cogl_matrix_stack_translate (modelview_stack,
|
2009-10-13 18:09:42 -04:00
|
|
|
0.0f, -1.0 * height, 0.0f);
|
2009-03-30 12:07:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
CoglFeatureFlags
|
2009-05-28 08:03:19 -04:00
|
|
|
cogl_get_features (void)
|
2009-03-30 12:07:31 -04:00
|
|
|
{
|
|
|
|
_COGL_GET_CONTEXT (ctx, 0);
|
|
|
|
|
2009-06-16 20:46:06 -04:00
|
|
|
if (cogl_debug_flags & COGL_DEBUG_DISABLE_VBOS)
|
|
|
|
ctx->feature_flags &= ~COGL_FEATURE_VBOS;
|
|
|
|
|
2009-03-30 12:07:31 -04:00
|
|
|
return ctx->feature_flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
cogl_features_available (CoglFeatureFlags features)
|
|
|
|
{
|
|
|
|
_COGL_GET_CONTEXT (ctx, 0);
|
|
|
|
|
|
|
|
if (!ctx->features_cached)
|
|
|
|
_cogl_features_init ();
|
|
|
|
|
|
|
|
return (ctx->feature_flags & features) == features;
|
|
|
|
}
|
|
|
|
|
2009-09-25 09:34:34 -04:00
|
|
|
/* XXX: This function should either be replaced with one returning
|
|
|
|
* integers, or removed/deprecated and make the
|
|
|
|
* _cogl_draw_buffer_get_viewport* functions public.
|
|
|
|
*/
|
2009-03-30 12:07:31 -04:00
|
|
|
void
|
|
|
|
cogl_get_viewport (float v[4])
|
|
|
|
{
|
2009-09-25 09:34:34 -04:00
|
|
|
CoglHandle draw_buffer;
|
|
|
|
int viewport[4];
|
|
|
|
int i;
|
|
|
|
|
2009-07-01 07:57:30 -04:00
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
2009-03-30 12:07:31 -04:00
|
|
|
|
2009-09-25 09:34:34 -04:00
|
|
|
draw_buffer = _cogl_get_draw_buffer ();
|
|
|
|
_cogl_draw_buffer_get_viewport4fv (draw_buffer, viewport);
|
|
|
|
|
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
v[i] = viewport[i];
|
2009-03-30 12:07:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-05-19 09:44:29 -04:00
|
|
|
cogl_get_bitmasks (gint *red,
|
|
|
|
gint *green,
|
|
|
|
gint *blue,
|
|
|
|
gint *alpha)
|
2009-03-30 12:07:31 -04:00
|
|
|
{
|
|
|
|
GLint value;
|
2009-05-19 09:44:29 -04:00
|
|
|
|
2009-03-30 12:07:31 -04:00
|
|
|
if (red)
|
|
|
|
{
|
|
|
|
GE( glGetIntegerv(GL_RED_BITS, &value) );
|
|
|
|
*red = value;
|
|
|
|
}
|
2009-05-19 09:44:29 -04:00
|
|
|
|
2009-03-30 12:07:31 -04:00
|
|
|
if (green)
|
|
|
|
{
|
|
|
|
GE( glGetIntegerv(GL_GREEN_BITS, &value) );
|
|
|
|
*green = value;
|
|
|
|
}
|
2009-05-19 09:44:29 -04:00
|
|
|
|
2009-03-30 12:07:31 -04:00
|
|
|
if (blue)
|
|
|
|
{
|
|
|
|
GE( glGetIntegerv(GL_BLUE_BITS, &value) );
|
|
|
|
*blue = value;
|
|
|
|
}
|
2009-05-19 09:44:29 -04:00
|
|
|
|
2009-03-30 12:07:31 -04:00
|
|
|
if (alpha)
|
|
|
|
{
|
|
|
|
GE( glGetIntegerv(GL_ALPHA_BITS, &value ) );
|
|
|
|
*alpha = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cogl_set_fog (const CoglColor *fog_color,
|
|
|
|
CoglFogMode mode,
|
|
|
|
float density,
|
|
|
|
float z_near,
|
|
|
|
float z_far)
|
|
|
|
{
|
|
|
|
GLfloat fogColor[4];
|
|
|
|
GLenum gl_mode = GL_LINEAR;
|
|
|
|
|
[cogl] Improving Cogl journal to minimize driver overheads + GPU state changes
Previously the journal was always flushed at the end of
_cogl_rectangles_with_multitexture_coords, (i.e. the end of any
cogl_rectangle* calls) but now we have broadened the potential for batching
geometry. In ideal circumstances we will only flush once per scene.
In summary the journal works like this:
When you use any of the cogl_rectangle* APIs then nothing is emitted to the
GPU at this point, we just log one or more quads into the journal. A
journal entry consists of the quad coordinates, an associated material
reference, and a modelview matrix. Ideally the journal only gets flushed
once at the end of a scene, but in fact there are things to consider that
may cause unwanted flushing, including:
- modifying materials mid-scene
This is because each quad in the journal has an associated material
reference (i.e. not copy), so if you try and modify a material that is
already referenced in the journal we force a flush first)
NOTE: For now this means you should avoid using cogl_set_source_color()
since that currently uses a single shared material. Later we
should change it to use a pool of materials that is recycled
when the journal is flushed.
- modifying any state that isn't currently logged, such as depth, fog and
backface culling enables.
The first thing that happens when flushing, is to upload all the vertex data
associated with the journal into a single VBO.
We then go through a process of splitting up the journal into batches that
have compatible state so they can be emitted to the GPU together. This is
currently broken up into 3 levels so we can stagger the state changes:
1) we break the journal up according to changes in the number of material layers
associated with logged quads. The number of layers in a material determines
the stride of the associated vertices, so we have to update our vertex
array offsets at this level. (i.e. calling gl{Vertex,Color},Pointer etc)
2) we further split batches up according to material compatability. (e.g.
materials with different textures) We flush material state at this level.
3) Finally we split batches up according to modelview changes. At this level
we update the modelview matrix and actually emit the actual draw command.
This commit is largely about putting the initial design in-place; this will be
followed by other changes that take advantage of the extended batching.
2009-06-17 13:46:42 -04:00
|
|
|
/* The cogl journal doesn't currently track fog state changes */
|
|
|
|
_cogl_journal_flush ();
|
|
|
|
|
2009-03-30 12:07:31 -04:00
|
|
|
fogColor[0] = cogl_color_get_red_float (fog_color);
|
|
|
|
fogColor[1] = cogl_color_get_green_float (fog_color);
|
|
|
|
fogColor[2] = cogl_color_get_blue_float (fog_color);
|
|
|
|
fogColor[3] = cogl_color_get_alpha_float (fog_color);
|
|
|
|
|
|
|
|
glEnable (GL_FOG);
|
|
|
|
|
|
|
|
glFogfv (GL_FOG_COLOR, fogColor);
|
|
|
|
|
|
|
|
#if HAVE_COGL_GLES
|
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case COGL_FOG_MODE_LINEAR:
|
|
|
|
gl_mode = GL_LINEAR;
|
|
|
|
break;
|
|
|
|
case COGL_FOG_MODE_EXPONENTIAL:
|
|
|
|
gl_mode = GL_EXP;
|
|
|
|
break;
|
|
|
|
case COGL_FOG_MODE_EXPONENTIAL_SQUARED:
|
|
|
|
gl_mode = GL_EXP2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* TODO: support other modes for GLES2 */
|
|
|
|
|
|
|
|
/* NB: GLES doesn't have glFogi */
|
|
|
|
glFogf (GL_FOG_MODE, gl_mode);
|
|
|
|
glHint (GL_FOG_HINT, GL_NICEST);
|
|
|
|
|
|
|
|
glFogf (GL_FOG_DENSITY, (GLfloat) density);
|
|
|
|
glFogf (GL_FOG_START, (GLfloat) z_near);
|
|
|
|
glFogf (GL_FOG_END, (GLfloat) z_far);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cogl_disable_fog (void)
|
|
|
|
{
|
[cogl] Improving Cogl journal to minimize driver overheads + GPU state changes
Previously the journal was always flushed at the end of
_cogl_rectangles_with_multitexture_coords, (i.e. the end of any
cogl_rectangle* calls) but now we have broadened the potential for batching
geometry. In ideal circumstances we will only flush once per scene.
In summary the journal works like this:
When you use any of the cogl_rectangle* APIs then nothing is emitted to the
GPU at this point, we just log one or more quads into the journal. A
journal entry consists of the quad coordinates, an associated material
reference, and a modelview matrix. Ideally the journal only gets flushed
once at the end of a scene, but in fact there are things to consider that
may cause unwanted flushing, including:
- modifying materials mid-scene
This is because each quad in the journal has an associated material
reference (i.e. not copy), so if you try and modify a material that is
already referenced in the journal we force a flush first)
NOTE: For now this means you should avoid using cogl_set_source_color()
since that currently uses a single shared material. Later we
should change it to use a pool of materials that is recycled
when the journal is flushed.
- modifying any state that isn't currently logged, such as depth, fog and
backface culling enables.
The first thing that happens when flushing, is to upload all the vertex data
associated with the journal into a single VBO.
We then go through a process of splitting up the journal into batches that
have compatible state so they can be emitted to the GPU together. This is
currently broken up into 3 levels so we can stagger the state changes:
1) we break the journal up according to changes in the number of material layers
associated with logged quads. The number of layers in a material determines
the stride of the associated vertices, so we have to update our vertex
array offsets at this level. (i.e. calling gl{Vertex,Color},Pointer etc)
2) we further split batches up according to material compatability. (e.g.
materials with different textures) We flush material state at this level.
3) Finally we split batches up according to modelview changes. At this level
we update the modelview matrix and actually emit the actual draw command.
This commit is largely about putting the initial design in-place; this will be
followed by other changes that take advantage of the extended batching.
2009-06-17 13:46:42 -04:00
|
|
|
/* Currently the journal can't track changes to fog state... */
|
|
|
|
_cogl_journal_flush ();
|
|
|
|
|
2009-03-30 12:07:31 -04:00
|
|
|
glDisable (GL_FOG);
|
|
|
|
}
|
|
|
|
|
[cogl] Improving Cogl journal to minimize driver overheads + GPU state changes
Previously the journal was always flushed at the end of
_cogl_rectangles_with_multitexture_coords, (i.e. the end of any
cogl_rectangle* calls) but now we have broadened the potential for batching
geometry. In ideal circumstances we will only flush once per scene.
In summary the journal works like this:
When you use any of the cogl_rectangle* APIs then nothing is emitted to the
GPU at this point, we just log one or more quads into the journal. A
journal entry consists of the quad coordinates, an associated material
reference, and a modelview matrix. Ideally the journal only gets flushed
once at the end of a scene, but in fact there are things to consider that
may cause unwanted flushing, including:
- modifying materials mid-scene
This is because each quad in the journal has an associated material
reference (i.e. not copy), so if you try and modify a material that is
already referenced in the journal we force a flush first)
NOTE: For now this means you should avoid using cogl_set_source_color()
since that currently uses a single shared material. Later we
should change it to use a pool of materials that is recycled
when the journal is flushed.
- modifying any state that isn't currently logged, such as depth, fog and
backface culling enables.
The first thing that happens when flushing, is to upload all the vertex data
associated with the journal into a single VBO.
We then go through a process of splitting up the journal into batches that
have compatible state so they can be emitted to the GPU together. This is
currently broken up into 3 levels so we can stagger the state changes:
1) we break the journal up according to changes in the number of material layers
associated with logged quads. The number of layers in a material determines
the stride of the associated vertices, so we have to update our vertex
array offsets at this level. (i.e. calling gl{Vertex,Color},Pointer etc)
2) we further split batches up according to material compatability. (e.g.
materials with different textures) We flush material state at this level.
3) Finally we split batches up according to modelview changes. At this level
we update the modelview matrix and actually emit the actual draw command.
This commit is largely about putting the initial design in-place; this will be
followed by other changes that take advantage of the extended batching.
2009-06-17 13:46:42 -04:00
|
|
|
void
|
2009-06-29 12:10:34 -04:00
|
|
|
cogl_flush (void)
|
[cogl] Improving Cogl journal to minimize driver overheads + GPU state changes
Previously the journal was always flushed at the end of
_cogl_rectangles_with_multitexture_coords, (i.e. the end of any
cogl_rectangle* calls) but now we have broadened the potential for batching
geometry. In ideal circumstances we will only flush once per scene.
In summary the journal works like this:
When you use any of the cogl_rectangle* APIs then nothing is emitted to the
GPU at this point, we just log one or more quads into the journal. A
journal entry consists of the quad coordinates, an associated material
reference, and a modelview matrix. Ideally the journal only gets flushed
once at the end of a scene, but in fact there are things to consider that
may cause unwanted flushing, including:
- modifying materials mid-scene
This is because each quad in the journal has an associated material
reference (i.e. not copy), so if you try and modify a material that is
already referenced in the journal we force a flush first)
NOTE: For now this means you should avoid using cogl_set_source_color()
since that currently uses a single shared material. Later we
should change it to use a pool of materials that is recycled
when the journal is flushed.
- modifying any state that isn't currently logged, such as depth, fog and
backface culling enables.
The first thing that happens when flushing, is to upload all the vertex data
associated with the journal into a single VBO.
We then go through a process of splitting up the journal into batches that
have compatible state so they can be emitted to the GPU together. This is
currently broken up into 3 levels so we can stagger the state changes:
1) we break the journal up according to changes in the number of material layers
associated with logged quads. The number of layers in a material determines
the stride of the associated vertices, so we have to update our vertex
array offsets at this level. (i.e. calling gl{Vertex,Color},Pointer etc)
2) we further split batches up according to material compatability. (e.g.
materials with different textures) We flush material state at this level.
3) Finally we split batches up according to modelview changes. At this level
we update the modelview matrix and actually emit the actual draw command.
This commit is largely about putting the initial design in-place; this will be
followed by other changes that take advantage of the extended batching.
2009-06-17 13:46:42 -04:00
|
|
|
{
|
|
|
|
_cogl_journal_flush ();
|
|
|
|
}
|
|
|
|
|
2009-06-17 09:30:44 -04:00
|
|
|
void
|
|
|
|
cogl_read_pixels (int x,
|
|
|
|
int y,
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
CoglReadPixelsFlags source,
|
|
|
|
CoglPixelFormat format,
|
|
|
|
guint8 *pixels)
|
|
|
|
{
|
2009-10-22 11:55:07 -04:00
|
|
|
CoglHandle draw_buffer;
|
2009-10-21 18:24:28 -04:00
|
|
|
int draw_buffer_height;
|
2009-09-25 09:34:34 -04:00
|
|
|
int rowstride = width * 4;
|
|
|
|
guint8 *temprow;
|
|
|
|
|
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
2009-06-17 09:30:44 -04:00
|
|
|
|
|
|
|
g_return_if_fail (format == COGL_PIXEL_FORMAT_RGBA_8888);
|
|
|
|
g_return_if_fail (source == COGL_READ_PIXELS_COLOR_BUFFER);
|
|
|
|
|
2009-06-16 16:29:21 -04:00
|
|
|
temprow = g_alloca (rowstride * sizeof (guint8));
|
|
|
|
|
2009-10-22 11:55:07 -04:00
|
|
|
draw_buffer = _cogl_get_draw_buffer ();
|
2009-11-01 20:10:02 -05:00
|
|
|
|
|
|
|
_cogl_draw_buffer_flush_state (draw_buffer, 0);
|
|
|
|
|
2009-10-22 11:55:07 -04:00
|
|
|
draw_buffer_height = _cogl_draw_buffer_get_height (draw_buffer);
|
2009-06-17 09:30:44 -04:00
|
|
|
|
|
|
|
/* The y co-ordinate should be given in OpenGL's coordinate system
|
2009-11-01 20:12:10 -05:00
|
|
|
* so 0 is the bottom row
|
|
|
|
*
|
|
|
|
* NB: all offscreen rendering is done upside down so no conversion
|
|
|
|
* is necissary in this case.
|
|
|
|
*/
|
|
|
|
if (!cogl_is_offscreen (draw_buffer))
|
|
|
|
y = draw_buffer_height - y - height;
|
2009-06-17 09:30:44 -04:00
|
|
|
|
2009-11-01 20:11:21 -05:00
|
|
|
/* make sure any batched primitives get emitted to the GL driver before
|
|
|
|
* issuing our read pixels... */
|
|
|
|
cogl_flush ();
|
|
|
|
|
2009-06-17 09:30:44 -04:00
|
|
|
/* Setup the pixel store parameters that may have been changed by
|
|
|
|
Cogl */
|
2009-11-03 07:54:45 -05:00
|
|
|
GE (glPixelStorei (GL_PACK_ALIGNMENT, 1));
|
2009-06-17 09:30:44 -04:00
|
|
|
#ifdef HAVE_COGL_GL
|
2009-11-03 07:54:45 -05:00
|
|
|
GE (glPixelStorei (GL_PACK_ROW_LENGTH, 0));
|
|
|
|
GE (glPixelStorei (GL_PACK_SKIP_PIXELS, 0));
|
|
|
|
GE (glPixelStorei (GL_PACK_SKIP_ROWS, 0));
|
2009-06-17 09:30:44 -04:00
|
|
|
#endif /* HAVE_COGL_GL */
|
|
|
|
|
2009-11-03 07:54:45 -05:00
|
|
|
GE (glReadPixels (x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels));
|
2009-06-17 09:30:44 -04:00
|
|
|
|
2009-10-22 11:55:07 -04:00
|
|
|
/* NB: All offscreen rendering is done upside down so there is no need
|
|
|
|
* to flip in this case... */
|
|
|
|
if (cogl_is_offscreen (draw_buffer))
|
|
|
|
return;
|
|
|
|
|
2009-06-17 09:30:44 -04:00
|
|
|
/* TODO: consider using the GL_MESA_pack_invert extension in the future
|
|
|
|
* to avoid this flip... */
|
|
|
|
|
|
|
|
/* vertically flip the buffer in-place */
|
|
|
|
for (y = 0; y < height / 2; y++)
|
|
|
|
{
|
|
|
|
if (y != height - y - 1) /* skip center row */
|
|
|
|
{
|
|
|
|
memcpy (temprow,
|
|
|
|
pixels + y * rowstride, rowstride);
|
|
|
|
memcpy (pixels + y * rowstride,
|
|
|
|
pixels + (height - y - 1) * rowstride, rowstride);
|
|
|
|
memcpy (pixels + (height - y - 1) * rowstride,
|
|
|
|
temprow,
|
|
|
|
rowstride);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-29 17:32:05 -04:00
|
|
|
void
|
|
|
|
cogl_begin_gl (void)
|
|
|
|
{
|
|
|
|
CoglMaterialFlushOptions options;
|
2009-11-09 06:50:16 -05:00
|
|
|
gulong enable_flags = 0;
|
2009-06-29 17:32:05 -04:00
|
|
|
int i;
|
|
|
|
|
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
|
|
|
if (ctx->in_begin_gl_block)
|
|
|
|
{
|
|
|
|
static gboolean shown = FALSE;
|
|
|
|
if (!shown)
|
|
|
|
g_warning ("You should not nest cogl_begin_gl/cogl_end_gl blocks");
|
|
|
|
shown = TRUE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ctx->in_begin_gl_block = TRUE;
|
|
|
|
|
|
|
|
/* Flush all batched primitives */
|
|
|
|
cogl_flush ();
|
|
|
|
|
2009-09-25 09:34:34 -04:00
|
|
|
/* Flush framebuffer state, including clip state, modelview and
|
|
|
|
* projection matrix state
|
|
|
|
*
|
|
|
|
* NB: _cogl_draw_buffer_flush_state may disrupt various state (such
|
|
|
|
* as the material state) when flushing the clip stack, so should
|
|
|
|
* always be done first when preparing to draw. */
|
|
|
|
_cogl_draw_buffer_flush_state (_cogl_get_draw_buffer (), 0);
|
2009-06-29 17:32:05 -04:00
|
|
|
|
|
|
|
/* Setup the state for the current material */
|
|
|
|
|
|
|
|
/* We considered flushing a specific, minimal material here to try and
|
|
|
|
* simplify the GL state, but decided to avoid special cases and second
|
|
|
|
* guessing what would be actually helpful.
|
|
|
|
*
|
|
|
|
* A user should instead call cogl_set_source_color4ub() before
|
|
|
|
* cogl_begin_gl() to simplify the state flushed.
|
|
|
|
*/
|
|
|
|
options.flags = 0;
|
|
|
|
_cogl_material_flush_gl_state (ctx->source_material, &options);
|
|
|
|
|
|
|
|
/* FIXME: This api is a bit yukky, ideally it will be removed if we
|
|
|
|
* re-work the cogl_enable mechanism */
|
|
|
|
enable_flags |= _cogl_material_get_cogl_enable_flags (ctx->source_material);
|
|
|
|
|
|
|
|
if (ctx->enable_backface_culling)
|
|
|
|
enable_flags |= COGL_ENABLE_BACKFACE_CULLING;
|
|
|
|
|
|
|
|
cogl_enable (enable_flags);
|
2009-10-22 14:01:52 -04:00
|
|
|
_cogl_flush_face_winding ();
|
2009-06-29 17:32:05 -04:00
|
|
|
|
|
|
|
/* Disable all client texture coordinate arrays */
|
|
|
|
for (i = 0; i < ctx->n_texcoord_arrays_enabled; i++)
|
|
|
|
{
|
|
|
|
GE (glClientActiveTexture (GL_TEXTURE0 + i));
|
|
|
|
GE (glDisableClientState (GL_TEXTURE_COORD_ARRAY));
|
|
|
|
}
|
|
|
|
ctx->n_texcoord_arrays_enabled = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cogl_end_gl (void)
|
|
|
|
{
|
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
|
|
|
if (!ctx->in_begin_gl_block)
|
|
|
|
{
|
|
|
|
static gboolean shown = FALSE;
|
|
|
|
if (!shown)
|
|
|
|
g_warning ("cogl_end_gl is being called before cogl_begin_gl");
|
|
|
|
shown = TRUE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ctx->in_begin_gl_block = FALSE;
|
|
|
|
}
|
|
|
|
|
2009-09-28 21:58:27 -04:00
|
|
|
static CoglTextureUnit *
|
|
|
|
_cogl_texture_unit_new (void)
|
|
|
|
{
|
|
|
|
CoglTextureUnit *unit = g_new0 (CoglTextureUnit, 1);
|
|
|
|
unit->matrix_stack = _cogl_matrix_stack_new ();
|
|
|
|
return unit;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_cogl_texture_unit_free (CoglTextureUnit *unit)
|
|
|
|
{
|
|
|
|
_cogl_matrix_stack_destroy (unit->matrix_stack);
|
|
|
|
g_free (unit);
|
|
|
|
}
|
|
|
|
|
|
|
|
CoglTextureUnit *
|
|
|
|
_cogl_get_texture_unit (int index_)
|
|
|
|
{
|
|
|
|
GList *l;
|
|
|
|
CoglTextureUnit *unit;
|
|
|
|
|
|
|
|
_COGL_GET_CONTEXT (ctx, NULL);
|
|
|
|
|
|
|
|
for (l = ctx->texture_units; l; l = l->next)
|
|
|
|
{
|
|
|
|
unit = l->data;
|
|
|
|
|
|
|
|
if (unit->index == index_)
|
|
|
|
return unit;
|
|
|
|
|
|
|
|
/* The units are always sorted, so at this point we know this unit
|
|
|
|
* doesn't exist */
|
|
|
|
if (unit->index > index_)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* NB: if we now insert a new layer before l, that will maintain order.
|
|
|
|
*/
|
|
|
|
|
|
|
|
unit = _cogl_texture_unit_new ();
|
|
|
|
|
|
|
|
/* Note: see comment after for() loop above */
|
|
|
|
ctx->texture_units =
|
|
|
|
g_list_insert_before (ctx->texture_units, l, unit);
|
|
|
|
|
|
|
|
return unit;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_cogl_destroy_texture_units (void)
|
|
|
|
{
|
|
|
|
GList *l;
|
|
|
|
|
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
|
|
|
for (l = ctx->texture_units; l; l = l->next)
|
|
|
|
_cogl_texture_unit_free (l->data);
|
|
|
|
g_list_free (ctx->texture_units);
|
|
|
|
}
|
|
|
|
|
2009-10-13 18:09:42 -04:00
|
|
|
void
|
|
|
|
cogl_push_matrix (void)
|
|
|
|
{
|
2009-09-25 09:34:34 -04:00
|
|
|
CoglMatrixStack *modelview_stack =
|
|
|
|
_cogl_draw_buffer_get_modelview_stack (_cogl_get_draw_buffer ());
|
|
|
|
_cogl_matrix_stack_push (modelview_stack);
|
2009-10-13 18:09:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cogl_pop_matrix (void)
|
|
|
|
{
|
2009-09-25 09:34:34 -04:00
|
|
|
CoglMatrixStack *modelview_stack =
|
|
|
|
_cogl_draw_buffer_get_modelview_stack (_cogl_get_draw_buffer ());
|
|
|
|
_cogl_matrix_stack_pop (modelview_stack);
|
2009-10-13 18:09:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cogl_scale (float x, float y, float z)
|
|
|
|
{
|
2009-09-25 09:34:34 -04:00
|
|
|
CoglMatrixStack *modelview_stack =
|
|
|
|
_cogl_draw_buffer_get_modelview_stack (_cogl_get_draw_buffer ());
|
|
|
|
_cogl_matrix_stack_scale (modelview_stack, x, y, z);
|
2009-10-13 18:09:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cogl_translate (float x, float y, float z)
|
|
|
|
{
|
2009-09-25 09:34:34 -04:00
|
|
|
CoglMatrixStack *modelview_stack =
|
|
|
|
_cogl_draw_buffer_get_modelview_stack (_cogl_get_draw_buffer ());
|
|
|
|
_cogl_matrix_stack_translate (modelview_stack, x, y, z);
|
2009-10-13 18:09:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cogl_rotate (float angle, float x, float y, float z)
|
|
|
|
{
|
2009-09-25 09:34:34 -04:00
|
|
|
CoglMatrixStack *modelview_stack =
|
|
|
|
_cogl_draw_buffer_get_modelview_stack (_cogl_get_draw_buffer ());
|
|
|
|
_cogl_matrix_stack_rotate (modelview_stack, angle, x, y, z);
|
2009-10-13 18:09:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cogl_perspective (float fov_y,
|
|
|
|
float aspect,
|
|
|
|
float z_near,
|
|
|
|
float z_far)
|
|
|
|
{
|
|
|
|
float ymax = z_near * tanf (fov_y * G_PI / 360.0);
|
|
|
|
|
|
|
|
cogl_frustum (-ymax * aspect, /* left */
|
|
|
|
ymax * aspect, /* right */
|
|
|
|
-ymax, /* bottom */
|
|
|
|
ymax, /* top */
|
|
|
|
z_near,
|
|
|
|
z_far);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cogl_frustum (float left,
|
|
|
|
float right,
|
|
|
|
float bottom,
|
|
|
|
float top,
|
|
|
|
float z_near,
|
|
|
|
float z_far)
|
|
|
|
{
|
2009-09-25 09:34:34 -04:00
|
|
|
CoglMatrixStack *projection_stack =
|
|
|
|
_cogl_draw_buffer_get_projection_stack (_cogl_get_draw_buffer ());
|
2009-10-13 18:09:42 -04:00
|
|
|
|
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
2009-09-25 09:34:34 -04:00
|
|
|
_cogl_matrix_stack_load_identity (projection_stack);
|
2009-10-13 18:09:42 -04:00
|
|
|
|
2009-09-25 09:34:34 -04:00
|
|
|
_cogl_matrix_stack_frustum (projection_stack,
|
2009-10-13 18:09:42 -04:00
|
|
|
left,
|
|
|
|
right,
|
|
|
|
bottom,
|
|
|
|
top,
|
|
|
|
z_near,
|
|
|
|
z_far);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cogl_ortho (float left,
|
|
|
|
float right,
|
|
|
|
float bottom,
|
|
|
|
float top,
|
|
|
|
float z_near,
|
|
|
|
float z_far)
|
|
|
|
{
|
|
|
|
CoglMatrix ortho;
|
2009-09-25 09:34:34 -04:00
|
|
|
CoglMatrixStack *projection_stack =
|
|
|
|
_cogl_draw_buffer_get_projection_stack (_cogl_get_draw_buffer ());
|
2009-10-13 18:09:42 -04:00
|
|
|
|
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
|
|
|
cogl_matrix_init_identity (&ortho);
|
|
|
|
cogl_matrix_ortho (&ortho, left, right, bottom, top, z_near, z_far);
|
2009-09-25 09:34:34 -04:00
|
|
|
_cogl_matrix_stack_set (projection_stack, &ortho);
|
2009-10-13 18:09:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cogl_get_modelview_matrix (CoglMatrix *matrix)
|
|
|
|
{
|
2009-09-25 09:34:34 -04:00
|
|
|
CoglMatrixStack *modelview_stack =
|
|
|
|
_cogl_draw_buffer_get_modelview_stack (_cogl_get_draw_buffer ());
|
|
|
|
_cogl_matrix_stack_get (modelview_stack, matrix);
|
2009-10-26 07:01:33 -04:00
|
|
|
_COGL_MATRIX_DEBUG_PRINT (matrix);
|
2009-10-13 18:09:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cogl_set_modelview_matrix (CoglMatrix *matrix)
|
|
|
|
{
|
2009-09-25 09:34:34 -04:00
|
|
|
CoglMatrixStack *modelview_stack =
|
|
|
|
_cogl_draw_buffer_get_modelview_stack (_cogl_get_draw_buffer ());
|
|
|
|
_cogl_matrix_stack_set (modelview_stack, matrix);
|
2009-10-26 07:01:33 -04:00
|
|
|
_COGL_MATRIX_DEBUG_PRINT (matrix);
|
2009-10-13 18:09:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cogl_get_projection_matrix (CoglMatrix *matrix)
|
|
|
|
{
|
2009-09-25 09:34:34 -04:00
|
|
|
CoglMatrixStack *projection_stack =
|
|
|
|
_cogl_draw_buffer_get_projection_stack (_cogl_get_draw_buffer ());
|
|
|
|
_cogl_matrix_stack_get (projection_stack, matrix);
|
2009-10-26 07:01:33 -04:00
|
|
|
_COGL_MATRIX_DEBUG_PRINT (matrix);
|
2009-10-13 18:09:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cogl_set_projection_matrix (CoglMatrix *matrix)
|
|
|
|
{
|
2009-09-25 09:34:34 -04:00
|
|
|
CoglMatrixStack *projection_stack =
|
|
|
|
_cogl_draw_buffer_get_projection_stack (_cogl_get_draw_buffer ());
|
|
|
|
_cogl_matrix_stack_set (projection_stack, matrix);
|
2009-10-13 18:09:42 -04:00
|
|
|
|
|
|
|
/* FIXME: Update the inverse projection matrix!! Presumably use
|
|
|
|
* of clip planes must currently be broken if this API is used. */
|
2009-10-26 07:01:33 -04:00
|
|
|
_COGL_MATRIX_DEBUG_PRINT (matrix);
|
2009-10-13 18:09:42 -04:00
|
|
|
}
|
|
|
|
|
2009-09-25 09:34:34 -04:00
|
|
|
CoglClipStackState *
|
|
|
|
_cogl_get_clip_state (void)
|
2009-10-13 18:09:42 -04:00
|
|
|
{
|
2009-09-25 09:34:34 -04:00
|
|
|
CoglHandle draw_buffer;
|
|
|
|
|
|
|
|
draw_buffer = _cogl_get_draw_buffer ();
|
|
|
|
return _cogl_draw_buffer_get_clip_state (draw_buffer);
|
2009-10-13 18:09:42 -04:00
|
|
|
}
|
|
|
|
|
2009-11-11 08:26:54 -05:00
|
|
|
GQuark
|
|
|
|
_cogl_driver_error_quark (void)
|
|
|
|
{
|
|
|
|
return g_quark_from_static_string ("cogl-driver-error-quark");
|
|
|
|
}
|