[Mesa-dev] [PATCH 2/2] _mesa_meta_GenerateMipmap: Generate separate shaders for glsl 120 / 130

Ian Romanick idr at freedesktop.org
Tue Sep 11 00:58:30 PDT 2012


On 09/05/2012 05:43 AM, Anuj Phogat wrote:
> glsl version of _mesa_meta_GenerateMipmap() would require separate
> shaders for glsl 120 and 130.
>
> NOTE: This is a candidate for stable branches.
>
> Signed-off-by: Anuj Phogat <anuj.phogat at gmail.com>
> ---
>   src/mesa/drivers/common/meta.c |  149 ++++++++++++++++++++++++----------------
>   1 files changed, 90 insertions(+), 59 deletions(-)
>
> diff --git a/src/mesa/drivers/common/meta.c b/src/mesa/drivers/common/meta.c
> index 7d701f4..4203aba 100644
> --- a/src/mesa/drivers/common/meta.c
> +++ b/src/mesa/drivers/common/meta.c
> @@ -292,6 +292,7 @@ struct gen_mipmap_state
>    */
>   struct glsl_sampler {
>      const char *type;
> +   const char *int_type;
>      const char *func;
>      const char *texcoords;
>   };
> @@ -3018,30 +3019,38 @@ setup_texture_sampler(GLenum target, struct glsl_sampler *sampler)
>      switch(target) {
>      case GL_TEXTURE_1D:
>         sampler->type = "sampler1D";
> +      sampler->int_type = "isampler1D";
>         sampler->func = "texture1D";
>         sampler->texcoords = "texCoords.x";
>         break;
>      case GL_TEXTURE_2D:
>         sampler->type = "sampler2D";
> +      sampler->int_type = "isampler2D";
>         sampler->func = "texture2D";
>         sampler->texcoords = "texCoords.xy";
>         break;
>      case GL_TEXTURE_3D:
>         sampler->type = "sampler3D";
> +      sampler->int_type = "isampler3D";
>         sampler->func = "texture3D";
>         sampler->texcoords = "texCoords";
>         break;
>      case GL_TEXTURE_CUBE_MAP:
>         sampler->type = "samplerCube";
> +      sampler->int_type = "isamplerCube";
>         sampler->func = "textureCube";
>         sampler->texcoords = "texCoords";
>         break;
>      case GL_TEXTURE_1D_ARRAY:
> -      sampler->type = "sampler1DARRAY";
> +      sampler->type = "sampler1DArray";

Uh... this should be in the first patch, right? :)

> +      sampler->int_type = "isampler1DArray";
> +      sampler->func = "texture1DArray";
>         sampler->texcoords = "texCoords.xy";
>         break;
>      case GL_TEXTURE_2D_ARRAY:
> -      sampler->type = "sampler2DARRAY";
> +      sampler->type = "sampler2DArray";

Same here.

> +      sampler->int_type = "isampler2DArray";
> +      sampler->func = "texture2DArray";
>         sampler->texcoords = "texCoords";
>         break;
>      default:
> @@ -3060,50 +3069,58 @@ setup_glsl_generate_mipmap(struct gl_context *ctx,
>         GLfloat x, y, tex[3];
>      };
>      struct glsl_sampler sampler;
> -
> -   static const char *vs_source =
> -      "attribute vec2 position;\n"
> -      "attribute vec3 textureCoords;\n"
> -      "varying vec3 texCoords;\n"
> -      "void main()\n"
> -      "{\n"
> -      "   texCoords = textureCoords;\n"
> -      "   gl_Position = vec4(position, 0.0, 1.0);\n"
> -      "}\n";
> -   static const char *fs_template =
> -      "#define SAMPLER_TYPE %s\n"
> -      "#define SAMPLER_FUNCTION %s\n"
> -      "#define TEX_COORDS %s\n"
> -      "uniform SAMPLER_TYPE texSampler;\n"
> -      "varying vec3 texCoords;\n"
> -      "void main()\n"
> -      "{\n"
> -      "   gl_FragColor = SAMPLER_FUNCTION(texSampler, TEX_COORDS);\n"
> -      "}\n";
> -
> -   static const char *vs_int_source =
> -      "#version 130\n"
> -      "in vec2 position;\n"
> -      "in vec3 textureCoords;\n"
> -      "out vec3 texCoords;\n"
> -      "void main()\n"
> -      "{\n"
> -      "   texCoords = textureCoords;\n"
> -      "   gl_Position = vec4(position, 0.0, 1.0);\n"
> -      "}\n";
> -   static const char *fs_int_template =
> -      "#version 130\n"
> -      "#define SAMPLER_TYPE i%s\n"
> -      "#define TEX_COORDS %s\n"
> -      "uniform SAMPLER_TYPE texSampler;\n"
> -      "in vec3 texCoords;\n"
> -      "out ivec4 out_color;\n"
> -      "\n"
> -      "void main()\n"
> -      "{\n"
> -      "   out_color = texture(texSampler, TEX_COORDS);\n"
> -      "}\n";
> -   char *fs_source, *fs_int_source;
> +   static const char *vs_source;
> +   static const char *fs_template;
> +   static const char *glsl_out_type = "vec4";
> +
> +  if (ctx->Const.GLSLVersion < 130) {
> +     vs_source =
> +        "attribute vec2 position;\n"
> +        "attribute vec3 textureCoords;\n"
> +        "varying vec3 texCoords;\n"
> +        "void main()\n"
> +        "{\n"
> +        "   texCoords = textureCoords;\n"
> +        "   gl_Position = vec4(position, 0.0, 1.0);\n"
> +        "}\n";
> +     fs_template =
> +        "#extension GL_EXT_texture_array : %s\n"
> +        "#define SAMPLER_TYPE %s\n"
> +        "#define SAMPLER_FUNCTION %s\n"
> +        "#define TEX_COORDS %s\n"
> +        "uniform SAMPLER_TYPE texSampler;\n"
> +        "varying vec3 texCoords;\n"
> +        "void main()\n"
> +        "{\n"
> +        "   gl_FragColor = SAMPLER_FUNCTION(texSampler, TEX_COORDS);\n"
> +        "}\n";
> +  } else {
> +     vs_source =
> +        "#version 130\n"
> +        "in vec2 position;\n"
> +        "in vec3 textureCoords;\n"
> +        "out vec3 texCoords;\n"
> +        "void main()\n"
> +        "{\n"
> +        "   texCoords = textureCoords;\n"
> +        "   gl_Position = vec4(position, 0.0, 1.0);\n"
> +        "}\n";
> +     fs_template =
> +        "#version 130\n"
> +        "#define SAMPLER_TYPE %s\n"
> +        "#define OUT_TYPE %s\n"
> +        "#define TEX_COORDS %s\n"
> +        "uniform SAMPLER_TYPE texSampler;\n"
> +        "in vec3 texCoords;\n"
> +        "out OUT_TYPE out_color;\n"
> +        "\n"
> +        "void main()\n"
> +        "{\n"
> +        "   out_color = texture(texSampler, TEX_COORDS);\n"
> +        "}\n";
> +   }
> +   char *fs_source;
> +   const char *extension_mode;
>      unsigned fs_alloc_len;
>      GLuint vs, fs;
>
> @@ -3126,13 +3143,26 @@ setup_glsl_generate_mipmap(struct gl_context *ctx,
>
>      /* Generate a fragment shader program appropriate for the texture target */
>      setup_texture_sampler(target, &sampler);
> -   fs_alloc_len = strlen(fs_template) + strlen(sampler.type) +
> -                  strlen(sampler.func) + strlen(sampler.texcoords) + 1;
> -   fs_source = (char *) malloc(fs_alloc_len);
>
> -   sprintf(fs_source, fs_template,
> -           sampler.type, sampler.func, sampler.texcoords);
> +   if(ctx->Const.GLSLVersion < 130) {
> +      extension_mode = ((target == GL_TEXTURE_1D_ARRAY) ||
> +	                (target == GL_TEXTURE_2D_ARRAY)) ?
> +			"require" : "disable";
>
> +      fs_alloc_len = strlen(fs_template) + strlen(extension_mode) +
> +                     strlen(sampler.type) + strlen(sampler.func) +
> +		     strlen(sampler.texcoords) + 1;
> +      fs_source = (char *) malloc(fs_alloc_len);
> +      sprintf(fs_source, fs_template,
> +              extension_mode, sampler.type, sampler.func, sampler.texcoords);
> +   }
> +   else {
> +      fs_alloc_len = strlen(fs_template) + strlen(sampler.type) +
> +                     strlen(glsl_out_type) + strlen(sampler.texcoords) + 1;
> +      fs_source = (char *) malloc(fs_alloc_len);
> +      sprintf(fs_source, fs_template,
> +              sampler.type, glsl_out_type, sampler.texcoords);
> +   }
>      vs = compile_shader_with_debug(ctx, GL_VERTEX_SHADER, vs_source);
>      fs = compile_shader_with_debug(ctx, GL_FRAGMENT_SHADER, fs_source);
>
> @@ -3150,18 +3180,19 @@ setup_glsl_generate_mipmap(struct gl_context *ctx,
>
>      if ((_mesa_is_desktop_gl(ctx) && ctx->Const.GLSLVersion >= 130) ||
>          _mesa_is_gles3(ctx)){
> +      glsl_out_type = "ivec4";
>         /* Generate a fragment shader program appropriate for the texture
>          * target
>          */
> -      fs_alloc_len = strlen(fs_int_template) + strlen(sampler.type) +
> -                     strlen(sampler.texcoords) + 1;
> -      fs_int_source = (char *) malloc(fs_alloc_len);
> +      fs_alloc_len = strlen(fs_template) + strlen(sampler.int_type) +
> +                     strlen(glsl_out_type) + strlen(sampler.texcoords) + 1;
> +      fs_source = (char *) malloc(fs_alloc_len);
>
> -      sprintf(fs_int_source, fs_int_template,
> -              sampler.type, sampler.texcoords);
> +      sprintf(fs_source, fs_template,
> +              sampler.int_type, glsl_out_type, sampler.texcoords);
>
> -      vs = compile_shader_with_debug(ctx, GL_VERTEX_SHADER, vs_int_source);
> -      fs = compile_shader_with_debug(ctx, GL_FRAGMENT_SHADER, fs_int_source);
> +      vs = compile_shader_with_debug(ctx, GL_VERTEX_SHADER, vs_source);
> +      fs = compile_shader_with_debug(ctx, GL_FRAGMENT_SHADER, fs_source);
>
>         mipmap->IntegerShaderProg = _mesa_CreateProgramObjectARB();
>         _mesa_AttachShader(mipmap->IntegerShaderProg, fs);
> @@ -3176,7 +3207,7 @@ setup_glsl_generate_mipmap(struct gl_context *ctx,
>          * BindFragDataLocation to 0.
>          */
>         link_program_with_debug(ctx, mipmap->IntegerShaderProg);
> -      free(fs_int_source);
> +      free(fs_source);
>      }
>   }
>
>



More information about the mesa-dev mailing list