[virglrenderer-devel] [PATCH 1/3] vrend: Support GLES shaders v2

Jakob Bornecrantz jakob at collabora.com
Fri Feb 23 17:32:42 UTC 2018


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

v2: Do not create a global state but instead add field
vrend_shader_cfg and send that into more functions.

Signed-off-by: Elie Tournier <elie.tournier at collabora.com>
Signed-off-by: Jakob Bornecrantz <jakob at collabora.com>
---
 src/vrend_renderer.c |  5 +--
 src/vrend_shader.c   | 99 ++++++++++++++++++++++++++++------------------------
 src/vrend_shader.h   |  4 ++-
 3 files changed, 59 insertions(+), 49 deletions(-)

diff --git a/src/vrend_renderer.c b/src/vrend_renderer.c
index 6d51684..1bf15f5 100644
--- a/src/vrend_renderer.c
+++ b/src/vrend_renderer.c
@@ -856,11 +856,11 @@ static struct vrend_linked_shader_program *add_shader_program(struct vrend_conte
       bool ret;
 
       if (gs)
-         vrend_patch_vertex_shader_interpolants(gs->glsl_prog,
+         vrend_patch_vertex_shader_interpolants(&ctx->shader_cfg, gs->glsl_prog,
                                                 &gs->sel->sinfo,
                                                 &fs->sel->sinfo, true, fs->key.flatshade);
       else
-         vrend_patch_vertex_shader_interpolants(vs->glsl_prog,
+         vrend_patch_vertex_shader_interpolants(&ctx->shader_cfg, vs->glsl_prog,
                                                 &vs->sel->sinfo,
                                                 &fs->sel->sinfo, false, fs->key.flatshade);
       ret = vrend_compile_shader(ctx, gs ? gs : vs);
@@ -4264,6 +4264,7 @@ struct vrend_context *vrend_create_context(int id, uint32_t nlen, const char *de
 
    grctx->res_hash = vrend_object_init_ctx_table();
 
+   grctx->shader_cfg.use_gles = vrend_state.use_gles;
    grctx->shader_cfg.use_core_profile = vrend_state.use_core_profile;
    grctx->shader_cfg.use_explicit_locations = vrend_state.use_explicit_locations;
    vrend_renderer_create_sub_ctx(grctx, 0);
diff --git a/src/vrend_shader.c b/src/vrend_shader.c
index f3cee7a..f8cee35 100644
--- a/src/vrend_shader.c
+++ b/src/vrend_shader.c
@@ -31,6 +31,7 @@
 #include <math.h>
 #include <errno.h>
 #include "vrend_shader.h"
+
 extern int vrend_dump_shaders;
 
 /* start convert of tgsi to glsl */
@@ -2194,48 +2195,51 @@ 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 (ctx->uses_gpu_shader5)
-      STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_gpu_shader5 : require\n");
-
+   if (ctx->cfg->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");
+      if (ctx->uses_gpu_shader5)
+         STRCAT_WITH_RET(glsl_hdr, "#extension GL_ARB_gpu_shader5 : require\n");
+   }
    return glsl_hdr;
 }
 
@@ -2276,11 +2280,14 @@ const char *vrend_shader_samplertypeconv(int sampler_type, int *is_shad)
    }
 }
 
-static const char *get_interp_string(int interpolate, bool flatshade)
+static const char *get_interp_string(struct vrend_shader_cfg *cfg, int interpolate, bool flatshade)
 {
    switch (interpolate) {
    case TGSI_INTERPOLATE_LINEAR:
+   if (!cfg->use_gles)
       return "noperspective ";
+   else
+      return "";
    case TGSI_INTERPOLATE_PERSPECTIVE:
       return "smooth ";
    case TGSI_INTERPOLATE_CONSTANT:
@@ -2344,7 +2351,7 @@ static char *emit_ios(struct dump_ctx *ctx, char *glsl_hdr)
          if (ctx->prog_type == TGSI_PROCESSOR_FRAGMENT &&
              (ctx->inputs[i].name == TGSI_SEMANTIC_GENERIC ||
               ctx->inputs[i].name == TGSI_SEMANTIC_COLOR)) {
-            prefix = get_interp_string(ctx->inputs[i].interpolate, ctx->key->flatshade);
+            prefix = get_interp_string(ctx->cfg, ctx->inputs[i].interpolate, ctx->key->flatshade);
             if (!prefix)
                prefix = "";
             ctx->num_interps++;
@@ -2686,7 +2693,7 @@ static void replace_interp(char *program,
    memcpy(ptr, pstring, strlen(pstring));
 }
 
-bool vrend_patch_vertex_shader_interpolants(char *program,
+bool vrend_patch_vertex_shader_interpolants(struct vrend_shader_cfg *cfg, char *program,
                                             struct vrend_shader_info *vs_info,
                                             struct vrend_shader_info *fs_info, bool is_gs, bool flatshade)
 {
@@ -2700,7 +2707,7 @@ bool vrend_patch_vertex_shader_interpolants(char *program,
       return true;
 
    for (i = 0; i < fs_info->num_interps; i++) {
-      pstring = get_interp_string(fs_info->interpinfo[i].interpolate, flatshade);
+      pstring = get_interp_string(cfg, fs_info->interpinfo[i].interpolate, flatshade);
       if (!pstring)
          continue;
 
diff --git a/src/vrend_shader.h b/src/vrend_shader.h
index 1e4c7a3..f0dd9cb 100644
--- a/src/vrend_shader.h
+++ b/src/vrend_shader.h
@@ -71,11 +71,13 @@ struct vrend_shader_key {
 
 struct vrend_shader_cfg {
    int glsl_version;
+   bool use_gles;
    bool use_core_profile;
    bool use_explicit_locations;
 };
 
-bool vrend_patch_vertex_shader_interpolants(char *program,
+bool vrend_patch_vertex_shader_interpolants(struct vrend_shader_cfg *cfg,
+                                            char *program,
                                             struct vrend_shader_info *vs_info,
                                             struct vrend_shader_info *fs_info,
                                             bool is_gs, bool flatshade);
-- 
2.14.1



More information about the virglrenderer-devel mailing list