[mutter/wip/3-monitors-on-nvidia: 8/10] wip! renderer-native-gles3: add function for drawing pixels to egl surface



commit f2036d9e29691a9bb8fb63f0b4d11ceee7e554e0
Author: Ray Strode <rstrode redhat com>
Date:   Thu Sep 13 13:36:52 2018 -0400

    wip! renderer-native-gles3: add function for drawing pixels to egl surface

 src/backends/native/meta-renderer-native-gles3.c | 185 +++++++++++++++++++++++
 src/backends/native/meta-renderer-native-gles3.h |  10 ++
 2 files changed, 195 insertions(+)
---
diff --git a/src/backends/native/meta-renderer-native-gles3.c 
b/src/backends/native/meta-renderer-native-gles3.c
index dbc59b6f6..8fb53ec0e 100644
--- a/src/backends/native/meta-renderer-native-gles3.c
+++ b/src/backends/native/meta-renderer-native-gles3.c
@@ -178,6 +178,113 @@ paint_egl_image (MetaGles3   *gles3,
                                     GL_NEAREST));
 }
 
+gboolean
+meta_renderer_native_gles3_draw_pixels (MetaEgl        *egl,
+                                        MetaGles3      *gles3,
+                                        unsigned int    width,
+                                        unsigned int    height,
+                                        uint8_t        *pixels,
+                                        GError        **error)
+{
+  GLuint vertex_array;
+  GLuint vertex_buffer;
+  GLuint triangle_buffer;
+  static const float view_left = -1.0f, view_right = 1.0f, view_top = 1.0f, view_bottom = -1.0f;
+  static const float texture_left = 0.0f, texture_right = 1.0f, texture_top = 0.0f, texture_bottom = 1.0f;
+
+  struct __attribute__ ((__packed__)) position
+  {
+    float x, y;
+  };
+
+  struct __attribute__ ((__packed__)) texture_coordinate
+  {
+    float u, v;
+  };
+
+  struct __attribute__ ((__packed__)) vertex
+  {
+    struct position position;
+    struct texture_coordinate texture_coordinate;
+  };
+
+  struct __attribute__ ((__packed__)) triangle
+  {
+    unsigned int first_vertex;
+    unsigned int middle_vertex;
+    unsigned int last_vertex;
+  };
+
+  enum
+  {
+    RIGHT_TOP_VERTEX = 0,
+    BOTTOM_RIGHT_VERTEX,
+    BOTTOM_LEFT_VERTEX,
+    TOP_LEFT_VERTEX
+  };
+
+  struct vertex vertices[] = {
+    [RIGHT_TOP_VERTEX] = {{view_right, view_top},
+                          {texture_right, texture_top}},
+    [BOTTOM_RIGHT_VERTEX] = {{view_right, view_bottom},
+                             {texture_right, texture_bottom}},
+    [BOTTOM_LEFT_VERTEX] = {{view_left, view_bottom},
+                            {texture_left, texture_bottom}},
+    [TOP_LEFT_VERTEX] = {{view_left, view_top},
+                         {texture_left, texture_top}},
+  };
+
+  struct triangle triangles[] = {
+    {TOP_LEFT_VERTEX, BOTTOM_RIGHT_VERTEX, BOTTOM_LEFT_VERTEX},
+    {TOP_LEFT_VERTEX, RIGHT_TOP_VERTEX, BOTTOM_RIGHT_VERTEX},
+  };
+
+  GLuint texture;
+
+  meta_gles3_clear_error (gles3);
+
+  GLBAS (gles3, glClearColor, (0.0,1.0,0.0,1.0));
+  GLBAS (gles3, glClear, (GL_COLOR_BUFFER_BIT));
+
+  GLBAS (gles3, glViewport, (0, 0, width, height));
+  GLBAS (gles3, glGenVertexArrays, (1, &vertex_array));
+  GLBAS (gles3, glBindVertexArray, (vertex_array));
+
+  GLBAS (gles3, glGenBuffers, (1, &vertex_buffer));
+  GLBAS (gles3, glBindBuffer, (GL_ARRAY_BUFFER, vertex_buffer));
+  GLBAS (gles3, glBufferData, (GL_ARRAY_BUFFER, sizeof (vertices), vertices, GL_STREAM_DRAW));
+
+  GLBAS (gles3, glGenBuffers, (1, &triangle_buffer));
+  GLBAS (gles3, glBindBuffer, (GL_ELEMENT_ARRAY_BUFFER, triangle_buffer));
+  GLBAS (gles3, glBufferData, (GL_ELEMENT_ARRAY_BUFFER, sizeof (triangles), triangles, GL_STREAM_DRAW));
+
+  GLBAS (gles3, glActiveTexture, (GL_TEXTURE0));
+  GLBAS (gles3, glGenTextures, (1, &texture));
+  GLBAS (gles3, glBindTexture, (GL_TEXTURE_2D, texture));
+  GLBAS (gles3, glTexParameteri, (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
+                                  GL_NEAREST));
+  GLBAS (gles3, glTexParameteri, (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
+                                  GL_NEAREST));
+  GLBAS (gles3, glTexParameteri, (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
+                                  GL_CLAMP_TO_EDGE));
+  GLBAS (gles3, glTexParameteri, (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
+                                  GL_CLAMP_TO_EDGE));
+  GLBAS (gles3, glTexParameteri, (GL_TEXTURE_2D, GL_TEXTURE_WRAP_R_OES,
+                                  GL_CLAMP_TO_EDGE));
+  GLBAS (gles3, glTexImage2D, (GL_TEXTURE_2D, 0, GL_RGBA,
+                               width, height, 0, GL_RGBA,
+                               GL_UNSIGNED_BYTE, pixels));
+
+  GLBAS (gles3, glBindBuffer, (GL_ARRAY_BUFFER, vertex_buffer));
+  GLBAS (gles3, glEnableVertexAttribArray, (0));
+  GLBAS (gles3, glVertexAttribPointer, (0, 2, GL_FLOAT, GL_FALSE, sizeof (struct vertex), (void *) offsetof 
(struct vertex, position)));
+  GLBAS (gles3, glEnableVertexAttribArray, (1));
+  GLBAS (gles3, glVertexAttribPointer, (1, 2, GL_FLOAT, GL_FALSE, sizeof (struct vertex), (void *) offsetof 
(struct vertex, texture_coordinate)));
+  GLBAS (gles3, glDrawElements, (GL_TRIANGLES, G_N_ELEMENTS (triangles) * (sizeof (struct triangle) / sizeof 
(unsigned int)), GL_UNSIGNED_INT, 0));
+
+  return TRUE;
+}
+
 gboolean
 meta_renderer_native_gles3_blit_shared_bo (MetaEgl        *egl,
                                            MetaGles3      *gles3,
@@ -256,3 +363,81 @@ meta_renderer_native_gles3_read_pixels (MetaEgl   *egl,
                                    target_data + width * y * 4));
     }
 }
+
+void
+meta_renderer_native_gles3_load_basic_shaders (MetaEgl   *egl,
+                                               MetaGles3 *gles3)
+{
+  GLuint vertex_shader = 0, fragment_shader = 0, shader_program;
+  gboolean status = FALSE;
+  const char *vertex_shader_source =
+"#version 330 core\n"
+"layout (location = 0) in vec2 position;\n"
+"layout (location = 1) in vec2 input_texture_coords;\n"
+"out vec2 texture_coords;\n"
+"void main()\n"
+"{\n"
+"  gl_Position = vec4(position.x, position.y, 0.0f, 1.0f);\n"
+"  texture_coords = input_texture_coords;\n"
+"}\n";
+
+  const char *fragment_shader_source =
+"#version 330 core\n"
+"uniform sampler2D input_texture;\n"
+"in vec2 texture_coords;\n"
+"out vec4 output_color;\n"
+"void main()\n"
+"{\n"
+"  output_color = texture(input_texture, texture_coords);\n"
+"}\n";
+
+  vertex_shader = glCreateShader (GL_VERTEX_SHADER);
+  glShaderSource (vertex_shader, 1, &vertex_shader_source, NULL);
+  glCompileShader (vertex_shader);
+  glGetShaderiv (vertex_shader, GL_COMPILE_STATUS, &status);
+
+  if (!status)
+    {
+      char compile_log[1024] = "";
+      glGetShaderInfoLog (vertex_shader, sizeof (compile_log), NULL, compile_log);
+      g_warning ("vertex shader compilation failed:\n %s\n", compile_log);
+      goto out;
+    }
+
+  fragment_shader = glCreateShader (GL_FRAGMENT_SHADER);
+  glShaderSource (fragment_shader, 1, &fragment_shader_source, NULL);
+  glCompileShader (fragment_shader);
+  glGetShaderiv (fragment_shader, GL_COMPILE_STATUS, &status);
+
+  if (!status)
+    {
+      char compile_log[1024] = "";
+      glGetShaderInfoLog (fragment_shader, sizeof (compile_log), NULL, compile_log);
+      g_warning ("fragment shader compilation failed:\n %s\n", compile_log);
+      goto out;
+    }
+
+  shader_program = glCreateProgram ();
+  glAttachShader (shader_program, vertex_shader);
+  glAttachShader (shader_program, fragment_shader);
+  glLinkProgram (shader_program);
+
+  glGetProgramiv (shader_program, GL_LINK_STATUS, &status);
+
+  if (!status)
+    {
+      char link_log[1024] = "";
+
+      glGetProgramInfoLog (shader_program, sizeof (link_log), NULL, link_log);
+      g_warning ("shader link failed:\n %s\n", link_log);
+      goto out;
+    }
+
+  glUseProgram (shader_program);
+
+out:
+  if (vertex_shader)
+    glDeleteShader (vertex_shader);
+  if (fragment_shader)
+    glDeleteShader (fragment_shader);
+}
diff --git a/src/backends/native/meta-renderer-native-gles3.h 
b/src/backends/native/meta-renderer-native-gles3.h
index b4ca19285..50dc9282d 100644
--- a/src/backends/native/meta-renderer-native-gles3.h
+++ b/src/backends/native/meta-renderer-native-gles3.h
@@ -42,4 +42,14 @@ void meta_renderer_native_gles3_read_pixels (MetaEgl   *egl,
                                              int        height,
                                              uint8_t   *target_data);
 
+gboolean meta_renderer_native_gles3_draw_pixels (MetaEgl        *egl,
+                                                 MetaGles3      *gles3,
+                                                 unsigned int    width,
+                                                 unsigned int    height,
+                                                 uint8_t        *pixels,
+                                                 GError        **error);
+
+void meta_renderer_native_gles3_load_basic_shaders (MetaEgl   *egl,
+                                                    MetaGles3 *gles3);
+
 #endif /* META_RENDERER_NATIVE_GLES3_H */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]