[Mesa-dev] [PATCH] glsl: add support for textureSize

Kenneth Graunke kenneth at whitecape.org
Sun Jun 19 02:18:31 PDT 2011


On 06/19/2011 01:24 AM, Dave Airlie wrote:
> From: Dave Airlie<airlied at redhat.com>
>
> I just used an opcode I called txs, TGSI has a RESINFO opcode this corresponds
> to.
>
> Signed-off-by: Dave Airlie<airlied at redhat.com>

Ah, sorry...I'd actually done all of this a few months ago, but got 
snagged trying to make ir_to_mesa not die a horrible death and never 
pushed it. :(

Please see the 'txs' branch of ~kwg/mesa.  (I rebased it but haven't 
re-tested...it at least compiles.)

I think my texture_builtins.py changes are a bit cleaner, and also adds 
the isampler/usampler cases (your patch uses generate_sigs rather than 
generate_fiu_sigs, so it'll only hit the floating point sampler types). 
  I'd prefer to use my changes in this case.

A few other things I think you might've missed:

You probably need to change ir_clone to only clone ir->coordinate if 
it's != NULL.  Otherwise you'll segfault when cloning TXS.

You also need more print visitor changes: don't print the coordinate, 
offset, projector, or shadow comparitor for TXS.  (Otherwise your reader 
code won't be able to re-read it...and I suspect you'll crash!)

Other than that, our two patchsets are largely the same.  A few 
differences in the reader code, but I don't really care one way or another.

With those few issues resolved, I think this should be good to go. 
Again, sorry for the duplicated effort.  I'll be offline all this week, 
so don't wait for further review from me.

--Kenneth

> ---
>   src/glsl/builtins/profiles/130.frag         |    2 -
>   src/glsl/builtins/tools/texture_builtins.py |   59 +++++++++++++-----
>   src/glsl/ir.cpp                             |    2 +-
>   src/glsl/ir.h                               |    4 +-
>   src/glsl/ir_clone.cpp                       |    1 +
>   src/glsl/ir_hv_accept.cpp                   |    9 ++-
>   src/glsl/ir_print_visitor.cpp               |    1 +
>   src/glsl/ir_reader.cpp                      |   89 ++++++++++++++------------
>   src/glsl/ir_rvalue_visitor.cpp              |    1 +
>   src/glsl/opt_tree_grafting.cpp              |    1 +
>   src/mesa/program/ir_to_mesa.cpp             |    1 +
>   11 files changed, 107 insertions(+), 63 deletions(-)
>
> diff --git a/src/glsl/builtins/profiles/130.frag b/src/glsl/builtins/profiles/130.frag
> index 0e3c7ac..c121859 100644
> --- a/src/glsl/builtins/profiles/130.frag
> +++ b/src/glsl/builtins/profiles/130.frag
> @@ -465,7 +465,6 @@ bvec4 not(bvec4 x);
>    * 8.7 - Texture Lookup Functions
>    */
>
> -#if 0
>   /* textureSize */
>   int   textureSize( sampler1D sampler, int lod);
>   int   textureSize(isampler1D sampler, int lod);
> @@ -496,7 +495,6 @@ ivec3 textureSize(usampler2DArray sampler, int lod);
>
>   ivec2 textureSize(sampler1DArrayShadow sampler, int lod);
>   ivec3 textureSize(sampler2DArrayShadow sampler, int lod);
> -#endif
>
>   /* texture - no bias */
>    vec4 texture( sampler1D sampler, float P);
> diff --git a/src/glsl/builtins/tools/texture_builtins.py b/src/glsl/builtins/tools/texture_builtins.py
> index a4054ca..ecd54b0 100755
> --- a/src/glsl/builtins/tools/texture_builtins.py
> +++ b/src/glsl/builtins/tools/texture_builtins.py
> @@ -49,20 +49,35 @@ def generate_sigs(g, tex_inst, sampler_type, variant = 0, unused_fields = 0):
>       extra_dim = get_extra_dim(sampler_type, variant&  Proj, unused_fields)
>       offset_dim = get_sampler_dim(sampler_type)
>
> -    if variant&  Single:
> -        return_type = "float"
> +    if tex_inst == "txs":
> +        txsrval = variant
> +        variant = 0
> +
> +    if tex_inst != "txs":
> +        if variant&  Single:
> +            return_type = "float"
> +        else:
> +            return_type = g + "vec4"
>       else:
> -        return_type = g + "vec4"
> +        if txsrval == 1:
> +            return_type = "int"
> +        elif txsrval == 2:
> +            return_type = "ivec2"
> +        elif txsrval == 3:
> +            return_type = "ivec3"
>
>       # Print parameters
>       print "   (signature", return_type
>       print "     (parameters"
>       print "       (declare (in) " + g + "sampler" + sampler_type + " sampler)"
> -    print "       (declare (in) " + vec_type("i" if tex_inst == "txf" else "", coord_dim + extra_dim) + " P)",
> +    if tex_inst != "txs":
> +        print "       (declare (in) " + vec_type("i" if tex_inst == "txf" else "", coord_dim + extra_dim) + " P)",
>       if tex_inst == "txl":
>           print "\n       (declare (in) float lod)",
>       elif tex_inst == "txf":
>           print "\n       (declare (in) int lod)",
> +    elif tex_inst == "txs":
> +        print "       (declare (in) int lod)",
>       elif tex_inst == "txd":
>           grad_type = vec_type("", coord_dim)
>           print "\n       (declare (in) " + grad_type + " dPdx)",
> @@ -76,17 +91,17 @@ def generate_sigs(g, tex_inst, sampler_type, variant = 0, unused_fields = 0):
>       print ")\n     ((return (" + tex_inst, return_type, "(var_ref sampler)",
>
>       # Coordinate
> -    if extra_dim>  0:
> -        print "(swiz " + "xyzw"[:coord_dim] + " (var_ref P))",
> -    else:
> -        print "(var_ref P)",
> -
> -    if variant&  Offset:
> -        print "(var_ref offset)",
> -    else:
> -        print "0",
> +    if tex_inst != "txs":
> +        if extra_dim>  0:
> +            print "(swiz " + "xyzw"[:coord_dim] + " (var_ref P))",
> +        else:
> +            print "(var_ref P)",
> +        if variant&  Offset:
> +           print "(var_ref offset)",
> +        else:
> +            print "0",
>
> -    if tex_inst != "txf":
> +    if tex_inst != "txf" and tex_inst != "txs":
>           # Projective divisor
>           if variant&  Proj:
>               print "(swiz " + "xyzw"[coord_dim + extra_dim-1] + " (var_ref P))",
> @@ -104,7 +119,7 @@ def generate_sigs(g, tex_inst, sampler_type, variant = 0, unused_fields = 0):
>       # Bias/explicit LOD/gradient:
>       if tex_inst == "txb":
>           print "(var_ref bias)",
> -    elif tex_inst == "txl" or tex_inst == "txf":
> +    elif tex_inst == "txl" or tex_inst == "txf" or tex_inst == "txs":
>           print "(var_ref lod)",
>       elif tex_inst == "txd":
>           print "((var_ref dPdx) (var_ref dPdy))",
> @@ -298,6 +313,20 @@ def generate_texture_functions(fs):
>       generate_sigs("", "txd", "2DArrayShadow", Offset | Single);
>       end_function(fs, "textureGradOffset")
>
> +    start_function("textureSize")
> +    generate_sigs("", "txs", "1D", 1)
> +    generate_sigs("", "txs", "2D", 2)
> +    generate_sigs("", "txs", "3D", 3)
> +    generate_sigs("", "txs", "Cube", 2)
> +    generate_sigs("", "txs", "1DShadow", 1);
> +    generate_sigs("", "txs", "2DShadow", 2);
> +    generate_sigs("", "txs", "CubeShadow", 2);
> +    generate_sigs("", "txs", "1DArray", 2)
> +    generate_sigs("", "txs", "2DArray", 3)
> +    generate_sigs("", "txs", "1DArrayShadow", 2);
> +    generate_sigs("", "txs", "2DArrayShadow", 3);
> +    end_function(fs, "textureSize")
> +
>       start_function("textureProjGrad")
>       generate_fiu_sigs("txd", "1D", Proj)
>       generate_fiu_sigs("txd", "1D", Proj, 2)
> diff --git a/src/glsl/ir.cpp b/src/glsl/ir.cpp
> index a3623b3..e58d067 100644
> --- a/src/glsl/ir.cpp
> +++ b/src/glsl/ir.cpp
> @@ -1128,7 +1128,7 @@ ir_dereference::is_lvalue()
>   }
>
>
> -const char *tex_opcode_strs[] = { "tex", "txb", "txl", "txd", "txf" };
> +const char *tex_opcode_strs[] = { "tex", "txb", "txl", "txd", "txf", "txs" };
>
>   const char *ir_texture::opcode_string()
>   {
> diff --git a/src/glsl/ir.h b/src/glsl/ir.h
> index a419843..18207a6 100644
> --- a/src/glsl/ir.h
> +++ b/src/glsl/ir.h
> @@ -1206,7 +1206,8 @@ enum ir_texture_opcode {
>      ir_txb,		/**<  Texture look-up with LOD bias */
>      ir_txl,		/**<  Texture look-up with explicit LOD */
>      ir_txd,		/**<  Texture look-up with partial derivatvies */
> -   ir_txf		/**<  Texel fetch with explicit LOD */
> +   ir_txf,		/**<  Texel fetch with explicit LOD */
> +   ir_txs		/**<  Texture size fetch */
>   };
>
>
> @@ -1227,6 +1228,7 @@ enum ir_texture_opcode {
>    * (txl<type>  <sampler>  <coordinate>  0 1 ( )<lod>)
>    * (txd<type>  <sampler>  <coordinate>  0 1 ( ) (dPdx dPdy))
>    * (txf<type>  <sampler>  <coordinate>  0<lod>)
> + * (txs<type>  <sampler>  <lod>)
>    */
>   class ir_texture : public ir_rvalue {
>   public:
> diff --git a/src/glsl/ir_clone.cpp b/src/glsl/ir_clone.cpp
> index 069bb85..e958099 100644
> --- a/src/glsl/ir_clone.cpp
> +++ b/src/glsl/ir_clone.cpp
> @@ -240,6 +240,7 @@ ir_texture::clone(void *mem_ctx, struct hash_table *ht) const
>         break;
>      case ir_txl:
>      case ir_txf:
> +   case ir_txs:
>         new_tex->lod_info.lod = this->lod_info.lod->clone(mem_ctx, ht);
>         break;
>      case ir_txd:
> diff --git a/src/glsl/ir_hv_accept.cpp b/src/glsl/ir_hv_accept.cpp
> index 4a607dc..7c11ffb 100644
> --- a/src/glsl/ir_hv_accept.cpp
> +++ b/src/glsl/ir_hv_accept.cpp
> @@ -171,9 +171,11 @@ ir_texture::accept(ir_hierarchical_visitor *v)
>      if (s != visit_continue)
>         return (s == visit_continue_with_parent) ? visit_continue : s;
>
> -   s = this->coordinate->accept(v);
> -   if (s != visit_continue)
> -      return (s == visit_continue_with_parent) ? visit_continue : s;
> +   if (this->coordinate) {
> +       s = this->coordinate->accept(v);
> +       if (s != visit_continue)
> +	   return (s == visit_continue_with_parent) ? visit_continue : s;
> +   }
>
>      if (this->projector) {
>         s = this->projector->accept(v);
> @@ -203,6 +205,7 @@ ir_texture::accept(ir_hierarchical_visitor *v)
>         break;
>      case ir_txl:
>      case ir_txf:
> +   case ir_txs:
>         s = this->lod_info.lod->accept(v);
>         if (s != visit_continue)
>   	 return (s == visit_continue_with_parent) ? visit_continue : s;
> diff --git a/src/glsl/ir_print_visitor.cpp b/src/glsl/ir_print_visitor.cpp
> index 5b5409d..a40a065 100644
> --- a/src/glsl/ir_print_visitor.cpp
> +++ b/src/glsl/ir_print_visitor.cpp
> @@ -270,6 +270,7 @@ void ir_print_visitor::visit(ir_texture *ir)
>         break;
>      case ir_txl:
>      case ir_txf:
> +   case ir_txs:
>         ir->lod_info.lod->accept(this);
>         break;
>      case ir_txd:
> diff --git a/src/glsl/ir_reader.cpp b/src/glsl/ir_reader.cpp
> index 30df257..1c45cfa 100644
> --- a/src/glsl/ir_reader.cpp
> +++ b/src/glsl/ir_reader.cpp
> @@ -883,6 +883,8 @@ ir_reader::read_texture(s_expression *expr)
>         { "tex", s_type, s_sampler, s_coord, s_offset, s_proj, s_shadow };
>      s_pattern txf_pattern[] =
>         { "txf", s_type, s_sampler, s_coord, s_offset, s_lod };
> +   s_pattern txs_pattern[] =
> +      { "txs", s_type, s_sampler, s_lod };
>      s_pattern other_pattern[] =
>         { tag, s_type, s_sampler, s_coord, s_offset, s_proj, s_shadow, s_lod };
>
> @@ -890,6 +892,8 @@ ir_reader::read_texture(s_expression *expr)
>         op = ir_tex;
>      } else if (MATCH(expr, txf_pattern)) {
>         op = ir_txf;
> +   } else if (MATCH(expr, txs_pattern)) {
> +      op = ir_txs;
>      } else if (MATCH(expr, other_pattern)) {
>         op = ir_texture::get_opcode(tag->value());
>         if (op == -1)
> @@ -918,47 +922,49 @@ ir_reader::read_texture(s_expression *expr)
>      }
>      tex->set_sampler(sampler, type);
>
> -   // Read coordinate (any rvalue)
> -   tex->coordinate = read_rvalue(s_coord);
> -   if (tex->coordinate == NULL) {
> -      ir_read_error(NULL, "when reading coordinate in (%s ...)",
> -		    tex->opcode_string());
> -      return NULL;
> -   }
> -
> -   // Read texel offset - either 0 or an rvalue.
> -   s_int *si_offset = SX_AS_INT(s_offset);
> -   if (si_offset == NULL || si_offset->value() != 0) {
> -      tex->offset = read_rvalue(s_offset);
> -      if (tex->offset == NULL) {
> -	 ir_read_error(s_offset, "expected 0 or an expression");
> -	 return NULL;
> -      }
> -   }
> -
> -   if (op != ir_txf) {
> -      s_int *proj_as_int = SX_AS_INT(s_proj);
> -      if (proj_as_int&&  proj_as_int->value() == 1) {
> -	 tex->projector = NULL;
> -      } else {
> -	 tex->projector = read_rvalue(s_proj);
> -	 if (tex->projector == NULL) {
> -	    ir_read_error(NULL, "when reading projective divide in (%s ..)",
> -	                  tex->opcode_string());
> -	    return NULL;
> -	 }
> -      }
> -
> -      if (s_shadow->subexpressions.is_empty()) {
> -	 tex->shadow_comparitor = NULL;
> -      } else {
> -	 tex->shadow_comparitor = read_rvalue(s_shadow);
> -	 if (tex->shadow_comparitor == NULL) {
> -	    ir_read_error(NULL, "when reading shadow comparitor in (%s ..)",
> -			  tex->opcode_string());
> -	    return NULL;
> -	 }
> -      }
> +   if (op != ir_txs) {
> +       // Read coordinate (any rvalue)
> +       tex->coordinate = read_rvalue(s_coord);
> +       if (tex->coordinate == NULL) {
> +	   ir_read_error(NULL, "when reading coordinate in (%s ...)",
> +			 tex->opcode_string());
> +	   return NULL;
> +       }
> +
> +       // Read texel offset - either 0 or an rvalue.
> +       s_int *si_offset = SX_AS_INT(s_offset);
> +       if (si_offset == NULL || si_offset->value() != 0) {
> +	   tex->offset = read_rvalue(s_offset);
> +	   if (tex->offset == NULL) {
> +	       ir_read_error(s_offset, "expected 0 or an expression");
> +	       return NULL;
> +	   }
> +       }
> +
> +       if (op != ir_txf) {
> +	   s_int *proj_as_int = SX_AS_INT(s_proj);
> +	   if (proj_as_int&&  proj_as_int->value() == 1) {
> +	       tex->projector = NULL;
> +	   } else {
> +	       tex->projector = read_rvalue(s_proj);
> +	       if (tex->projector == NULL) {
> +		   ir_read_error(NULL, "when reading projective divide in (%s ..)",
> +				 tex->opcode_string());
> +		   return NULL;
> +	       }
> +	   }
> +
> +	   if (s_shadow->subexpressions.is_empty()) {
> +	       tex->shadow_comparitor = NULL;
> +	   } else {
> +	       tex->shadow_comparitor = read_rvalue(s_shadow);
> +	       if (tex->shadow_comparitor == NULL) {
> +		   ir_read_error(NULL, "when reading shadow comparitor in (%s ..)",
> +				 tex->opcode_string());
> +		   return NULL;
> +	       }
> +	   }
> +       }
>      }
>
>      switch (op) {
> @@ -971,6 +977,7 @@ ir_reader::read_texture(s_expression *expr)
>         break;
>      case ir_txl:
>      case ir_txf:
> +   case ir_txs:
>         tex->lod_info.lod = read_rvalue(s_lod);
>         if (tex->lod_info.lod == NULL) {
>   	 ir_read_error(NULL, "when reading LOD in (%s ...)",
> diff --git a/src/glsl/ir_rvalue_visitor.cpp b/src/glsl/ir_rvalue_visitor.cpp
> index ed6c7cb..193bcd2 100644
> --- a/src/glsl/ir_rvalue_visitor.cpp
> +++ b/src/glsl/ir_rvalue_visitor.cpp
> @@ -63,6 +63,7 @@ ir_rvalue_visitor::visit_leave(ir_texture *ir)
>         break;
>      case ir_txf:
>      case ir_txl:
> +   case ir_txs:
>         handle_rvalue(&ir->lod_info.lod);
>         break;
>      case ir_txd:
> diff --git a/src/glsl/opt_tree_grafting.cpp b/src/glsl/opt_tree_grafting.cpp
> index 1ef940f..22a1749 100644
> --- a/src/glsl/opt_tree_grafting.cpp
> +++ b/src/glsl/opt_tree_grafting.cpp
> @@ -258,6 +258,7 @@ ir_tree_grafting_visitor::visit_enter(ir_texture *ir)
>         break;
>      case ir_txf:
>      case ir_txl:
> +   case ir_txs:
>         if (do_graft(&ir->lod_info.lod))
>   	 return visit_stop;
>         break;
> diff --git a/src/mesa/program/ir_to_mesa.cpp b/src/mesa/program/ir_to_mesa.cpp
> index 0086997..b44e56a 100644
> --- a/src/mesa/program/ir_to_mesa.cpp
> +++ b/src/mesa/program/ir_to_mesa.cpp
> @@ -2015,6 +2015,7 @@ ir_to_mesa_visitor::visit(ir_texture *ir)
>         dy = this->result;
>         break;
>      case ir_txf:
> +   case ir_txs:
>         assert(!"GLSL 1.30 features unsupported");
>         break;
>      }


More information about the mesa-dev mailing list