We don't need to split the wrapper snippet into two separate parts
because it should be ok to declare the flip uniform in the middle of
the shader as long as it is somewhere in the global scope. Therefore
we can just declare it right before the definition for the replacement
main function. This is important because we don't want to put anything
at the top of the application's shader in case it is using a
'#version' directive. In that case moving it to anything other than
the first line would break things.
This patch also adds a marker in a comment around the wrapper snippet
so that we can easily locate the snippet when glGetShaderSource is
called and remove it.
The wrapper for glGetAttachedShaders has been removed because there
are no longer any additional shaders attached to the program so we can
just let GL handle it directly.
Reviewed-by: Robert Bragg <robert@linux.intel.com>
(cherry picked from commit dbd92e24ae61dcbe7ef26f61c9117c5516a7fa87)
In our wrapper for glShaderSource we special case when a vertex shader
is being specified so we can sneak in a wrapper for the main function to
potentially flip all rendering upside down for better integration with
Cogl.
Previously we were appending the wrapper to all the sub-strings passed
via the vector of strings to glShaderSource but we now grow the vector
instead and insert the prelude and wrapper strings into the beginning
and end of the vector respectively so we should only have one copy for a
single shader.
Reviewed-by: Neil Roberts <neil@linux.intel.com>
(cherry picked from commit d2904d518718e3fbf4441abe2c2bcfd63edfd64b)
The SGX GLSL compiler refuses to accept shaders of the form:
void foo();
void bar() {
foo();
}
where foo is undefined at glShaderSource() time, left for definition at
link time. To work around this, simply append the wrapper shader to
user shaders, rather than building a separate shader that's always
linked with user shaders.
Signed-off-by: Daniel Stone <daniel@fooishbar.org>
Reviewed-by: Robert Bragg <robert@linux.intel.com>
(cherry picked from commit 96f02c445f763ace088be71dc32b3374b2cdbab2)
We want applications to fully control the lifetime of a CoglContext
without having to worry that internal resources (such as the default
2d,3d and rectangle textures, or any caches we maintain) could result in
circular references that keep the context alive. We also want to avoid
making CoglContext into a special kind of object that isn't ref-counted
or that can't be used with object apis such as
cogl_object_set_user_data. Being able to reliably destroy the context is
important on platforms such as Android where you may be required
bring-up and tear-down a CoglContext numerous times throughout the
applications lifetime. A dissadvantage of this policy is that it is now
possible to leave other object such as framebuffers in an inconsistent
state if the context is unreferenced and destroyed. The documentation
states that all objects that directly or indirectly depend on a context
that has been destroyed will be left in an inconsistent state and must
not be accessed thereafter. Applications (such as Android applications)
that need to cleanly destroy and re-create Cogl resources should make
sure to manually unref these dependant objects before destroying the
context.
Reviewed-by: Neil Roberts <neil@linux.intel.com>
(cherry picked from commit 23ce51beba1bb739a224e47614a59327dfbb65af)
When the CoglGLES2Context is bound to read from a CoglOffscreen then
the result will be upside down from what GL expects if
glCopyTexImage2D is used directly. To fix that, this patch now wraps
glCopyTexImage2D and glCopyTexSubImage2D so that the copy is doing by
binding an FBO to the target texture and then rendering a quad
sampling from the texture in the offscreen framebuffer.
The rendering is done using the Cogl context rather than the GLES2
context because otherwise it would have to do a fair bit of work to
try and stash the old state on the context before setting up the state
to do the blit. The down side of this is that the contexts need to be
synchronized so that the rendering will be up-to-date. As far as I
understand from the GL spec, this requires a glFinish and then the
texture needs to be rebound in the new context because updates to
shared objects are guaranteed to be reflected until the object is
rebound.
GLES2 supports using glCopyTexImage2D for cube map textures. As Cogl
doesn't currently have support for cube maps, it is quite hard to get
that to work with this patch. For now attempts to copy to a cube map
texture will just be sliently ignored.
This patch also includes a test case which renders an image to the
framebuffer and then copies it to a texture. The texture is then
rendered back to the framebuffer and the contents are checked for the
correct orientation using glReadPixels.
Reviewed-by: Robert Bragg <robert@linux.intel.com>
(cherry picked from commit 30b6da8134bad95267265e26685c7475f6c351c9)
This patch adds a hash table mapping texture object IDs to a struct so
that we can keep track of some of the state for each texture object.
Currently it will just track the width and height of the texture 2D
target.
Additionally it will now try to delete any texture objects that have
data created for them by the GLES2 context so that it won't leak them.
It only tracks objects that get data set on them, not all objects that
are bound because it is possible to use the GLES2 context with foreign
textures via cogl_gles2_texture_get_handle() and we don't want to
delete those.
In order to keep track of the currently bound texture object it also
needs to track the active texture unit.
Note that this state tracking will probably go wrong if GL throws an
error for invalid state. For example if glActiveTexture is called with
an invalid texture unit then GL will ignore the binding but Cogl will
assume it is valid and the state tracking will get out of sync.
Perhaps it would be good if Cogl could detect the errors but this is
difficult to do without consuming them.
Reviewed-by: Robert Bragg <robert@linux.intel.com>
(cherry picked from commit d8c72bb56cf3598fc57d629edc618f1bfa79f125)
Cogl has a different origin for texture coordinates than OpenGL so
that the results of rendering to a texture should leave the top of the
image at the texture coordinate 0,0 rather than the bottom. When a
GLES2 context is used to render to a Cogl texture via a CoglOffscreen
we don't really want the application to have to be aware of the
mismatch and flip the texture coordinates. To get that to work, this
patch now tracks all of the programs that the application generates
using the context and sneaks in an extra vertex shader with an
alternative main function. This main function multiplies the final
calculated gl_Position by a vector uniform which we can use to flip
the image. When the application uploads the source code for a vertex
shader we now replace any occurrences of the token 'main' with '_c31'
and this renamed function gets called from the replacement main
function. The token has a weird name so that it will be unlikely to
conflict with a variable name in the application's source but it also
needs to have the same number of characters as the original token so
that it won't affect column numbers in the error reporting.
We are also wrapping glGetShaderSource so that we can try to revert
the token name. The same goes for the error logs just in case the
error report mentions function names.
Both places that cause drawing to occur (glDrawElements and
glDrawArrays) are now also wrapped so that we can update the uniform
value whenever the program is used with a different type of
framebuffer from last time.
We additionally need to manually track the state for the viewport, the
stencil box and the front face because all of these will be affected
by whether we are flipping the image or not. Any attempts to change
these states will be queued and instead flushed at the last minute
before drawing.
There are still some known issues with this patch:
• glCopyTexImage2D and glCopyTexSubImage2D will do the wrong thing
when copying data from a CoglOffscreen. This could be quite fiddly
to solve.
• Point sprites won't flip correctly. To make this work we would need
to flip the gl_PointSprite builtin variable somehow. This is done in
the fragment shader not the vertex shader so flipping the calculated
gl_Position doesn't help here.
• The patch doesn't attempt to flip rendering to framebuffers for
textures created within the GLES2 context. This probably makes sense
because those textures are likely to be used within the GLES2
context in which case we want to leave the texture coordinates as
they are. However, if the texture is shared back out to Cogl with
cogl_gles2_texture_2d_new_from_handle then the texture will be
upside-down.
• The application can discover our secret uniform that we added via
glGetActiveUniform. It might be worth trying to disguise this by
wrapping that function although that could be quite fiddly.
Reviewed-by: Robert Bragg <robert@linux.intel.com>
(cherry picked from commit d589bf19e51f22c3241b2a18db10f22131ac126a)
All of the functions that create and destroy shaders are now wrapped
in the CoglGLES2Context so that we can track some extra data for them.
There are hash tables mapping object IDs to the corresponding data.
The data is currently not used for anything but will be in later
patches.
The glUseProgram, glAttachShader and glDetachShader functions
additionally need to be wrapped because GL does not delete shader
objects that are in use. Therefore we need to have a reference count
on the data so we can recognise when the last use has been removed.
The IDs are assumed to be specific to an individual CoglGLES2Context.
This is technically not the case because all of the CoglGLES2Contexts
are in the same share list. However we don't really want this to be
the case so currently we will assume sharing the object IDs between
contexts is undefined behaviour. Eventually we may want to actually
enforce this.
Reviewed-by: Robert Bragg <robert@linux.intel.com>
(cherry picked from commit 05dc1e34785ae5f5484cd398ecc5464bd8bd3dcd)
In GL, the default viewport and scissor should be set to the size of
the first surface that the context is bound to. If a CoglGLES2Context
is first used with an offscreen framebuffer then this surface will
actually be the dummy 1x1 window which will mess up the defaults. To
fix that, this patch makes it just always override the viewport and
scissor the first time the context is bound to something.
Reviewed-by: Robert Bragg <robert@linux.intel.com>
(cherry picked from commit 02567b3e6b64e6849b9f7c6aa2137401be7ece8d)
This makes it possible to integrate existing GLES2 code with
applications using Cogl as the rendering api.
Currently all GLES2 usage is handled with separate GLES2 contexts to
ensure that GLES2 api usage doesn't interfere with Cogl's own use of
OpenGL[ES]. The api has been designed though so we can provide tighter
integration later.
The api would allow us to support GLES2 virtualized on top of an
OpenGL/GLX driver as well as GLES2 virtualized on the core rendering api
of Cogl itself. Virtualizing the GLES2 support on Cogl will allow us to
take advantage of Cogl debugging facilities as well as let us optimize
the cost of allocating multiple GLES2 contexts and switching between
them which can both be very expensive with many drivers.
As as a side effect of this patch Cogl can also now be used as a
portable window system binding API for GLES2 as an alternative to EGL.
Parts of this patch are based on work done by Tomeu Vizoso
<tomeu.vizoso@collabora.com> who did the first iteration of adding GLES2
API support to Cogl so that WebGL support could be added to
webkit-clutter.
This patch adds a very minimal cogl-gles2-context example that shows how
to create a gles2 context, clear the screen to a random color and also
draw a triangle with the cogl api.
Reviewed-by: Neil Roberts <neil@linux.intel.com>
(cherry picked from commit 4bb6eff3dbd50d8fef7d6bdbed55c5aaa70036a8)