forked from AuroraMiddleware/gtk
Rework the GL renderer
This commit is contained in:
parent
5fa5008ee9
commit
6bce14bee2
@ -520,7 +520,7 @@ int
|
||||
gsk_gl_driver_create_vao_for_quad (GskGLDriver *driver,
|
||||
int position_id,
|
||||
int uv_id,
|
||||
int n_quads,
|
||||
int n_vertices,
|
||||
GskQuadVertex *quads)
|
||||
|
||||
{
|
||||
@ -530,7 +530,7 @@ gsk_gl_driver_create_vao_for_quad (GskGLDriver *driver,
|
||||
g_return_val_if_fail (GSK_IS_GL_DRIVER (driver), -1);
|
||||
g_return_val_if_fail (driver->in_frame, -1);
|
||||
|
||||
v = find_vao (driver->vaos, position_id, uv_id, n_quads, quads);
|
||||
v = find_vao (driver->vaos, position_id, uv_id, n_vertices, quads);
|
||||
if (v != NULL && !v->in_use)
|
||||
{
|
||||
GSK_NOTE (OPENGL, g_print ("Reusing VAO(%d)\n", v->vao_id));
|
||||
@ -543,17 +543,20 @@ gsk_gl_driver_create_vao_for_quad (GskGLDriver *driver,
|
||||
|
||||
glGenBuffers (1, &buffer_id);
|
||||
glBindBuffer (GL_ARRAY_BUFFER, buffer_id);
|
||||
glBufferData (GL_ARRAY_BUFFER, sizeof (GskQuadVertex) * n_quads, quads, GL_STATIC_DRAW);
|
||||
glBufferData (GL_ARRAY_BUFFER, sizeof (GskQuadVertex) * n_vertices, quads, GL_STATIC_DRAW);
|
||||
|
||||
glEnableVertexAttribArray (position_id);
|
||||
glVertexAttribPointer (position_id, 2, GL_FLOAT, GL_FALSE,
|
||||
sizeof (GskQuadVertex),
|
||||
(void *) G_STRUCT_OFFSET (GskQuadVertex, position));
|
||||
|
||||
glEnableVertexAttribArray (uv_id);
|
||||
glVertexAttribPointer (uv_id, 2, GL_FLOAT, GL_FALSE,
|
||||
sizeof (GskQuadVertex),
|
||||
(void *) G_STRUCT_OFFSET (GskQuadVertex, uv));
|
||||
if (uv_id != -1)
|
||||
{
|
||||
glEnableVertexAttribArray (uv_id);
|
||||
glVertexAttribPointer (uv_id, 2, GL_FLOAT, GL_FALSE,
|
||||
sizeof (GskQuadVertex),
|
||||
(void *) G_STRUCT_OFFSET (GskQuadVertex, uv));
|
||||
}
|
||||
|
||||
glBindBuffer (GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray (0);
|
||||
@ -563,8 +566,8 @@ gsk_gl_driver_create_vao_for_quad (GskGLDriver *driver,
|
||||
v->buffer_id = buffer_id;
|
||||
v->position_id = position_id;
|
||||
v->uv_id = uv_id;
|
||||
v->n_quads = n_quads;
|
||||
v->quads = g_memdup (quads, sizeof (GskQuadVertex) * n_quads);
|
||||
v->n_quads = n_vertices;
|
||||
v->quads = g_memdup (quads, sizeof (GskQuadVertex) * n_vertices);
|
||||
v->in_use = TRUE;
|
||||
g_hash_table_insert (driver->vaos, GINT_TO_POINTER (vao_id), v);
|
||||
|
||||
@ -572,8 +575,8 @@ gsk_gl_driver_create_vao_for_quad (GskGLDriver *driver,
|
||||
if (GSK_DEBUG_CHECK (OPENGL))
|
||||
{
|
||||
int i;
|
||||
g_print ("New VAO(%d) for quad[%d] : {\n", v->vao_id, n_quads);
|
||||
for (i = 0; i < n_quads; i++)
|
||||
g_print ("New VAO(%d) for quad[%d] : {\n", v->vao_id, n_vertices);
|
||||
for (i = 0; i < n_vertices; i++)
|
||||
{
|
||||
g_print (" { x:%.2f, y:%.2f } { u:%.2f, v:%.2f }\n",
|
||||
quads[i].position[0], quads[i].position[1],
|
||||
@ -641,6 +644,8 @@ gsk_gl_driver_create_render_target (GskGLDriver *driver,
|
||||
|
||||
g_array_append_val (t->fbos, f);
|
||||
|
||||
g_assert (glCheckFramebufferStatus (GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
|
||||
|
||||
glBindFramebuffer (GL_FRAMEBUFFER, driver->default_fbo.fbo_id);
|
||||
|
||||
return fbo_id;
|
||||
@ -719,7 +724,9 @@ gsk_gl_driver_bind_vao (GskGLDriver *driver,
|
||||
glBindVertexArray (v->vao_id);
|
||||
glBindBuffer (GL_ARRAY_BUFFER, v->buffer_id);
|
||||
glEnableVertexAttribArray (v->position_id);
|
||||
glEnableVertexAttribArray (v->uv_id);
|
||||
|
||||
if (v->uv_id != -1)
|
||||
glEnableVertexAttribArray (v->uv_id);
|
||||
|
||||
driver->bound_vao = v;
|
||||
}
|
||||
@ -757,9 +764,14 @@ gsk_gl_driver_bind_render_target (GskGLDriver *driver,
|
||||
}
|
||||
|
||||
out:
|
||||
status = glCheckFramebufferStatus (GL_FRAMEBUFFER);
|
||||
|
||||
return status == GL_FRAMEBUFFER_COMPLETE;
|
||||
if (texture_id != 0)
|
||||
{
|
||||
status = glCheckFramebufferStatus (GL_FRAMEBUFFER);
|
||||
g_assert_cmpint (status, ==, GL_FRAMEBUFFER_COMPLETE);
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void
|
||||
@ -856,6 +868,4 @@ gsk_gl_driver_init_texture_with_surface (GskGLDriver *driver,
|
||||
|
||||
if (t->min_filter != GL_NEAREST)
|
||||
glGenerateMipmap (GL_TEXTURE_2D);
|
||||
|
||||
glBindTexture (GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -41,8 +41,6 @@ shader_program_free (gpointer data)
|
||||
g_clear_pointer (&p->uniform_locations, g_hash_table_unref);
|
||||
g_clear_pointer (&p->attribute_locations, g_hash_table_unref);
|
||||
|
||||
glDeleteProgram (p->program_id);
|
||||
|
||||
g_slice_free (ShaderProgram, data);
|
||||
}
|
||||
|
||||
|
@ -5,6 +5,10 @@ gsk_private_source_shaders = [
|
||||
'resources/glsl/blit.vs.glsl',
|
||||
'resources/glsl/color.fs.glsl',
|
||||
'resources/glsl/color.vs.glsl',
|
||||
'resources/glsl/color_matrix.fs.glsl',
|
||||
'resources/glsl/color_matrix.vs.glsl',
|
||||
'resources/glsl/linear_gradient.fs.glsl',
|
||||
'resources/glsl/linear_gradient.vs.glsl',
|
||||
'resources/glsl/es2_common.fs.glsl',
|
||||
'resources/glsl/es2_common.vs.glsl',
|
||||
'resources/glsl/gl3_common.fs.glsl',
|
||||
|
@ -57,6 +57,5 @@ void main() {
|
||||
// Use red for debugging missing blend modes
|
||||
res = vec3(1.0, 0.0, 0.0);
|
||||
}
|
||||
|
||||
setOutputColor(vec4(res, Cs.a * uAlpha));
|
||||
setOutputColor(vec4(res, Cs.a) * uAlpha);
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
void main() {
|
||||
gl_Position = uMVP * vec4(aPosition, 0.0, 1.0);
|
||||
|
||||
// Flip the sampling
|
||||
vUv = vec2(aUv.x, aUv.y);
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
void main() {
|
||||
vec4 diffuse = Texture(uSource, vUv);
|
||||
|
||||
setOutputColor(vec4(diffuse.xyz, diffuse.a * uAlpha));
|
||||
setOutputColor(diffuse * uAlpha);
|
||||
}
|
||||
|
@ -1,5 +1,9 @@
|
||||
uniform vec4 uColor;
|
||||
|
||||
void main() {
|
||||
setOutputColor(uColor);
|
||||
vec4 color = uColor;
|
||||
|
||||
// Pre-multiply alpha
|
||||
color.rgb *= color.a;
|
||||
setOutputColor(color * uAlpha);
|
||||
}
|
||||
|
20
gsk/resources/glsl/color_matrix.fs.glsl
Normal file
20
gsk/resources/glsl/color_matrix.fs.glsl
Normal file
@ -0,0 +1,20 @@
|
||||
uniform mat4 uColorMatrix;
|
||||
uniform vec4 uColorOffset;
|
||||
|
||||
void main() {
|
||||
vec4 diffuse = Texture(uSource, vUv);
|
||||
vec4 color;
|
||||
|
||||
color = diffuse;
|
||||
|
||||
// Un-premultilpy
|
||||
if (color.a != 0.0)
|
||||
color.rgb /= color.a;
|
||||
|
||||
color = uColorMatrix * diffuse + uColorOffset;
|
||||
color = clamp(color, 0.0f, 1.0f);
|
||||
|
||||
color.rgb *= color.a;
|
||||
|
||||
setOutputColor(color * uAlpha);
|
||||
}
|
6
gsk/resources/glsl/color_matrix.vs.glsl
Normal file
6
gsk/resources/glsl/color_matrix.vs.glsl
Normal file
@ -0,0 +1,6 @@
|
||||
void main() {
|
||||
gl_Position = uMVP * vec4(aPosition, 0.0, 1.0);
|
||||
|
||||
// Flip the sampling
|
||||
vUv = vec2(aUv.x, aUv.y);
|
||||
}
|
@ -3,8 +3,9 @@ precision highp float;
|
||||
uniform sampler2D uSource;
|
||||
uniform sampler2D uMask;
|
||||
uniform mat4 uMVP;
|
||||
uniform float uAlpha;
|
||||
uniform float uAlpha = 1.0;
|
||||
uniform int uBlendMode;
|
||||
uniform vec4 uViewport;
|
||||
|
||||
in vec2 vUv;
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
uniform mat4 uMVP;
|
||||
uniform mat4 uProjection;
|
||||
|
||||
in vec2 aPosition;
|
||||
in vec2 aUv;
|
||||
|
43
gsk/resources/glsl/linear_gradient.fs.glsl
Normal file
43
gsk/resources/glsl/linear_gradient.fs.glsl
Normal file
@ -0,0 +1,43 @@
|
||||
uniform vec4 uColorStops[8];
|
||||
uniform float uColorOffsets[8];
|
||||
uniform int uNumColorStops;
|
||||
uniform vec2 uStartPoint;
|
||||
uniform vec2 uEndPoint;
|
||||
|
||||
vec4 fragCoord() {
|
||||
vec4 f = gl_FragCoord;
|
||||
f.x += uViewport.x;
|
||||
f.y = (uViewport.y + uViewport.w) - f.y;
|
||||
return f;
|
||||
}
|
||||
|
||||
void main() {
|
||||
float maxDist = length(uEndPoint - uStartPoint);
|
||||
|
||||
// Position relative to startPoint
|
||||
vec2 pos = fragCoord().xy - uStartPoint;
|
||||
|
||||
// Gradient direction
|
||||
vec2 gradient = uEndPoint - uStartPoint;
|
||||
float gradientLength = length(gradient);
|
||||
|
||||
// Current pixel, projected onto the line between the start point and the end point
|
||||
// The projection will be relative to the start point!
|
||||
vec2 proj = (dot(gradient, pos) / (gradientLength * gradientLength)) * gradient;
|
||||
|
||||
// Offset of the current pixel
|
||||
float offset = length(proj) / maxDist;
|
||||
|
||||
vec4 color = uColorStops[0];
|
||||
for (int i = 1; i < uNumColorStops; i ++) {
|
||||
if (offset >= uColorOffsets[i - 1]) {
|
||||
float o = (offset - uColorOffsets[i - 1]) / (uColorOffsets[i] - uColorOffsets[i - 1]);
|
||||
color = mix(uColorStops[i - 1], uColorStops[i], o);
|
||||
}
|
||||
}
|
||||
|
||||
/* Pre-multiply */
|
||||
color.rgb *= color.a;
|
||||
|
||||
setOutputColor(color * uAlpha);
|
||||
}
|
5
gsk/resources/glsl/linear_gradient.vs.glsl
Normal file
5
gsk/resources/glsl/linear_gradient.vs.glsl
Normal file
@ -0,0 +1,5 @@
|
||||
void main() {
|
||||
gl_Position = uMVP * vec4(aPosition, 0.0, 1.0);
|
||||
|
||||
vUv = vec2(aUv.x, aUv.y);
|
||||
}
|
Loading…
Reference in New Issue
Block a user