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



commit 97c7e05cb486de351e6767c960b400ae8102cfc2
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 |   9 ++
 2 files changed, 194 insertions(+)
---
diff --git a/src/backends/native/meta-renderer-native-gles3.c 
b/src/backends/native/meta-renderer-native-gles3.c
index 7afea8648..70b4facba 100644
--- a/src/backends/native/meta-renderer-native-gles3.c
+++ b/src/backends/native/meta-renderer-native-gles3.c
@@ -179,6 +179,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, sizeof (struct position) / sizeof (float), GL_FLOAT, GL_FALSE, 
sizeof (struct vertex), (void *) offsetof (struct vertex, position)));
+  GLBAS (gles3, glEnableVertexAttribArray, (1));
+  GLBAS (gles3, glVertexAttribPointer, (1, sizeof (struct texture_coordinate) / sizeof (float), 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,
@@ -238,3 +345,81 @@ meta_renderer_native_gles3_blit_shared_bo (MetaEgl        *egl,
 
   return TRUE;
 }
+
+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 4e7324c8d..2791302a0 100644
--- a/src/backends/native/meta-renderer-native-gles3.h
+++ b/src/backends/native/meta-renderer-native-gles3.h
@@ -37,4 +37,13 @@ gboolean meta_renderer_native_gles3_blit_shared_bo (MetaEgl       *egl,
                                                     struct gbm_bo *shared_bo,
                                                     GError       **error);
 
+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]