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)
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)