[virglrenderer-devel] [PATCH 6/8] vrend: Support GLES shaders.

Jakob Bornecrantz jakob.bornecrantz at collabora.com
Tue Feb 13 14:27:28 UTC 2018


Uses:
  * Always use the "#version 300 es" header
  * Set high precision by default
  * Do not use noperspective attribute

Signed-off-by: Elie Tournier <elie.tournier at collabora.com>
Signed-off-by: Jakob Bornecrantz <jakob.bornecrantz at collabora.com>
---
 src/virglrenderer.c |   5 ++-
 src/vrend_shader.c  | 104 ++++++++++++++++++++++++++++++++--------------------
 src/vrend_shader.h  |   2 +
 3 files changed, 71 insertions(+), 40 deletions(-)

diff --git a/src/virglrenderer.c b/src/virglrenderer.c
index c89cb60..3fca100 100644
--- a/src/virglrenderer.c
+++ b/src/virglrenderer.c
@@ -35,6 +35,7 @@
 #include "util/u_format.h"
 #include "util/u_math.h"
 #include "vrend_renderer.h"
+#include "vrend_shader.h"
 
 #include "virglrenderer.h"
 
@@ -325,7 +326,9 @@ int virgl_renderer_init(void *cookie, int flags, struct virgl_renderer_callbacks
    if (flags & VIRGL_RENDERER_THREAD_SYNC)
       renderer_flags |= VREND_USE_THREAD_SYNC;
 
-   return vrend_renderer_init(&virgl_cbs, renderer_flags);
+   int res = vrend_renderer_init(&virgl_cbs, renderer_flags);
+   res |= vrend_shader_init();
+   return res;
 }
 
 int virgl_renderer_get_fd_for_texture(uint32_t tex_id, int *fd)
diff --git a/src/vrend_shader.c b/src/vrend_shader.c
index b824140..cf720b1 100644
--- a/src/vrend_shader.c
+++ b/src/vrend_shader.c
@@ -31,6 +31,9 @@
 #include <math.h>
 #include <errno.h>
 #include "vrend_shader.h"
+
+#include <epoxy/gl.h>
+
 extern int vrend_dump_shaders;
 
 /* start convert of tgsi to glsl */
@@ -144,6 +147,22 @@ struct dump_ctx {
    bool uses_sample_shading;
 };
 
+struct global_renderer_shader_state {
+   bool inited;
+   bool use_gles;
+};
+
+static struct global_renderer_shader_state vrend_shader_state;
+
+int vrend_shader_init()
+{
+   /* make sure you have the latest version of libepoxy */
+   vrend_shader_state.use_gles = epoxy_is_desktop_gl() == 0;
+   vrend_shader_state.inited = true;
+
+   return 0;
+}
+
 static inline const char *tgsi_proc_to_prefix(int shader_type)
 {
    switch (shader_type) {
@@ -2142,45 +2161,49 @@ prolog(struct tgsi_iterate_context *iter)
 
 static char *emit_header(struct dump_ctx *ctx, char *glsl_hdr)
 {
-   if (ctx->prog_type == TGSI_PROCESSOR_GEOMETRY || ctx->glsl_ver_required == 150)
-      STRCAT_WITH_RET(glsl_hdr, "#version 150\n");
-
-   else if (ctx->glsl_ver_required == 140)
-      STRCAT_WITH_RET(glsl_hdr, "#version 140\n");
-   else
-      STRCAT_WITH_RET(glsl_hdr, "#version 130\n");
-   if (ctx->prog_type == TGSI_PROCESSOR_VERTEX && ctx->cfg->use_explicit_locations)
-      STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_explicit_attrib_location : enable\n");
-   if (ctx->prog_type == TGSI_PROCESSOR_FRAGMENT && fs_emit_layout(ctx))
-      STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_fragment_coord_conventions : enable\n");
-   if (ctx->glsl_ver_required < 140 && ctx->uses_sampler_rect)
-      STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_texture_rectangle : require\n");
-   if (ctx->uses_cube_array)
-      STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_texture_cube_map_array : require\n");
-   if (ctx->has_ints)
-      STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_shader_bit_encoding : require\n");
-   if (ctx->uses_sampler_ms)
-      STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_texture_multisample : require\n");
-   if (ctx->has_instanceid)
-      STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_draw_instanced : require\n");
-   if (ctx->num_ubo)
-      STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_uniform_buffer_object : require\n");
-   if (ctx->uses_lodq)
-      STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_texture_query_lod : require\n");
-   if (ctx->uses_txq_levels)
-      STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_texture_query_levels : require\n");
-   if (ctx->uses_tg4)
-      STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_texture_gather : require\n");
-   if (ctx->has_viewport_idx)
-      STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_viewport_array : require\n");
-   if (ctx->has_frag_viewport_idx)
-      STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_fragment_layer_viewport : require\n");
-   if (ctx->uses_stencil_export)
-      STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_shader_stencil_export : require\n");
-   if (ctx->uses_layer)
-      STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_fragment_layer_viewport : require\n");
-   if (ctx->uses_sample_shading)
-      STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_sample_shading : require\n");
+   if (vrend_shader_state.use_gles) {
+      STRCAT_WITH_RET(glsl_hdr, "#version 300 es\n");
+      STRCAT_WITH_RET(glsl_hdr, "precision highp float;\n");
+   } else{
+      if (ctx->prog_type == TGSI_PROCESSOR_GEOMETRY || ctx->glsl_ver_required == 150)
+         STRCAT_WITH_RET(glsl_hdr, "#version 150\n");
+      else if (ctx->glsl_ver_required == 140)
+         STRCAT_WITH_RET(glsl_hdr, "#version 140\n");
+      else
+         STRCAT_WITH_RET(glsl_hdr, "#version 130\n");
+      if (ctx->prog_type == TGSI_PROCESSOR_VERTEX && ctx->cfg->use_explicit_locations)
+         STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_explicit_attrib_location : enable\n");
+      if (ctx->prog_type == TGSI_PROCESSOR_FRAGMENT && fs_emit_layout(ctx))
+         STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_fragment_coord_conventions : enable\n");
+      if (ctx->glsl_ver_required < 140 && ctx->uses_sampler_rect)
+         STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_texture_rectangle : require\n");
+      if (ctx->uses_cube_array)
+         STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_texture_cube_map_array : require\n");
+      if (ctx->has_ints)
+         STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_shader_bit_encoding : require\n");
+      if (ctx->uses_sampler_ms)
+         STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_texture_multisample : require\n");
+      if (ctx->has_instanceid)
+         STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_draw_instanced : require\n");
+      if (ctx->num_ubo)
+         STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_uniform_buffer_object : require\n");
+      if (ctx->uses_lodq)
+         STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_texture_query_lod : require\n");
+      if (ctx->uses_txq_levels)
+         STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_texture_query_levels : require\n");
+      if (ctx->uses_tg4)
+         STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_texture_gather : require\n");
+      if (ctx->has_viewport_idx)
+         STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_viewport_array : require\n");
+      if (ctx->has_frag_viewport_idx)
+         STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_fragment_layer_viewport : require\n");
+      if (ctx->uses_stencil_export)
+         STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_shader_stencil_export : require\n");
+      if (ctx->uses_layer)
+         STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_fragment_layer_viewport : require\n");
+      if (ctx->uses_sample_shading)
+         STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_sample_shading : require\n");
+   }
    return glsl_hdr;
 }
 
@@ -2225,7 +2248,10 @@ static const char *get_interp_string(int interpolate, bool flatshade)
 {
    switch (interpolate) {
    case TGSI_INTERPOLATE_LINEAR:
+   if (!vrend_shader_state.use_gles)
       return "noperspective ";
+   else
+      return "";
    case TGSI_INTERPOLATE_PERSPECTIVE:
       return "smooth ";
    case TGSI_INTERPOLATE_CONSTANT:
diff --git a/src/vrend_shader.h b/src/vrend_shader.h
index 1e4c7a3..e358cad 100644
--- a/src/vrend_shader.h
+++ b/src/vrend_shader.h
@@ -75,6 +75,8 @@ struct vrend_shader_cfg {
    bool use_explicit_locations;
 };
 
+int vrend_shader_init();
+
 bool vrend_patch_vertex_shader_interpolants(char *program,
                                             struct vrend_shader_info *vs_info,
                                             struct vrend_shader_info *fs_info,
-- 
2.14.1



More information about the virglrenderer-devel mailing list