gallium/util: fix two MSVC compiler warnings
[mesa.git] / src / gallium / auxiliary / util / u_simple_shaders.c
index b46ec589eab7bcf42c2218405dfdd61a1e7ac143..d62a65579ae41e1267dfd39e0333f7d97b116e7b 100644 (file)
@@ -41,6 +41,7 @@
 #include "util/u_simple_shaders.h"
 #include "util/u_debug.h"
 #include "util/u_memory.h"
+#include "util/u_string.h"
 #include "tgsi/tgsi_dump.h"
 #include "tgsi/tgsi_strings.h"
 #include "tgsi/tgsi_ureg.h"
 void *
 util_make_vertex_passthrough_shader(struct pipe_context *pipe,
                                     uint num_attribs,
-                                    const uint *semantic_names,
+                                    const enum tgsi_semantic *semantic_names,
                                     const uint *semantic_indexes,
                                     bool window_space)
 {
    return util_make_vertex_passthrough_shader_with_so(pipe, num_attribs,
                                                       semantic_names,
                                                       semantic_indexes,
-                                                      window_space, NULL);
+                                                      window_space, false, NULL);
 }
 
 void *
 util_make_vertex_passthrough_shader_with_so(struct pipe_context *pipe,
                                     uint num_attribs,
-                                    const uint *semantic_names,
+                                    const enum tgsi_semantic *semantic_names,
                                     const uint *semantic_indexes,
-                                    bool window_space,
+                                    bool window_space, bool layered,
                                    const struct pipe_stream_output_info *so)
 {
    struct ureg_program *ureg;
@@ -99,6 +100,15 @@ util_make_vertex_passthrough_shader_with_so(struct pipe_context *pipe,
       ureg_MOV( ureg, dst, src );
    }
 
+   if (layered) {
+      struct ureg_src instance_id =
+         ureg_DECL_system_value(ureg, TGSI_SEMANTIC_INSTANCEID, 0);
+      struct ureg_dst layer = ureg_DECL_output(ureg, TGSI_SEMANTIC_LAYER, 0);
+
+      ureg_MOV(ureg, ureg_writemask(layer, TGSI_WRITEMASK_X),
+               ureg_scalar(instance_id, TGSI_SWIZZLE_X));
+   }
+
    ureg_END( ureg );
 
    return ureg_create_shader_with_so_and_destroy( ureg, pipe, so );
@@ -107,28 +117,13 @@ util_make_vertex_passthrough_shader_with_so(struct pipe_context *pipe,
 
 void *util_make_layered_clear_vertex_shader(struct pipe_context *pipe)
 {
-   static const char text[] =
-         "VERT\n"
-         "DCL IN[0]\n"
-         "DCL IN[1]\n"
-         "DCL SV[0], INSTANCEID\n"
-         "DCL OUT[0], POSITION\n"
-         "DCL OUT[1], GENERIC[0]\n"
-         "DCL OUT[2], LAYER\n"
-
-         "MOV OUT[0], IN[0]\n"
-         "MOV OUT[1], IN[1]\n"
-         "MOV OUT[2], SV[0]\n"
-         "END\n";
-   struct tgsi_token tokens[1000];
-   struct pipe_shader_state state;
+   const enum tgsi_semantic semantic_names[] = {TGSI_SEMANTIC_POSITION,
+                                                TGSI_SEMANTIC_GENERIC};
+   const unsigned semantic_indices[] = {0, 0};
 
-   if (!tgsi_text_translate(text, tokens, ARRAY_SIZE(tokens))) {
-      assert(0);
-      return NULL;
-   }
-   pipe_shader_state_from_tgsi(&state, tokens);
-   return pipe->create_vs_state(pipe, &state);
+   return util_make_vertex_passthrough_shader_with_so(pipe, 2, semantic_names,
+                                                      semantic_indices, false,
+                                                      true, NULL);
 }
 
 /**
@@ -204,6 +199,76 @@ void *util_make_layered_clear_geometry_shader(struct pipe_context *pipe)
    return pipe->create_gs_state(pipe, &state);
 }
 
+static void
+ureg_load_tex(struct ureg_program *ureg, struct ureg_dst out,
+              struct ureg_src coord, struct ureg_src sampler,
+              enum tgsi_texture_type tex_target,
+              bool load_level_zero, bool use_txf)
+{
+   if (use_txf) {
+      struct ureg_dst temp = ureg_DECL_temporary(ureg);
+
+      ureg_F2I(ureg, temp, coord);
+
+      if (load_level_zero)
+         ureg_TXF_LZ(ureg, out, tex_target, ureg_src(temp), sampler);
+      else
+         ureg_TXF(ureg, out, tex_target, ureg_src(temp), sampler);
+   } else {
+      if (load_level_zero)
+         ureg_TEX_LZ(ureg, out, tex_target, coord, sampler);
+      else
+         ureg_TEX(ureg, out, tex_target, coord, sampler);
+   }
+}
+
+/**
+ * Make simple fragment texture shader, with xrbias->float conversion:
+ *  IMM {1023/510, -384/510, 0, 1}
+ *  TEX TEMP[0], IN[0], SAMP[0], 2D;
+ *  MAD TEMP[0].xyz TEMP[0], IMM[0].xxxx, IMM[0].yyyy
+ *  MOV OUT[0], TEMP[0]
+ *  END;
+ *
+ * \param tex_target  one of PIPE_TEXTURE_x
+ */
+void *
+util_make_fragment_tex_shader_xrbias(struct pipe_context *pipe,
+                                     enum tgsi_texture_type tex_target)
+{
+   struct ureg_program *ureg;
+   struct ureg_src sampler;
+   struct ureg_src coord;
+   struct ureg_dst temp;
+   struct ureg_dst out;
+   struct ureg_src imm;
+   enum tgsi_return_type stype = TGSI_RETURN_TYPE_FLOAT;
+
+   ureg = ureg_create(PIPE_SHADER_FRAGMENT);
+   if (!ureg)
+      return NULL;
+
+   imm = ureg_imm4f(ureg, 1023.0f/510.0f, -384.0f/510.0f, 0.0f, 1.0f);
+   sampler = ureg_DECL_sampler(ureg, 0);
+   ureg_DECL_sampler_view(ureg, 0, tex_target, stype, stype, stype, stype);
+   coord = ureg_DECL_fs_input(ureg,
+                              TGSI_SEMANTIC_GENERIC, 0,
+                              TGSI_INTERPOLATE_LINEAR);
+   out = ureg_DECL_output(ureg, TGSI_SEMANTIC_COLOR, 0);
+   temp = ureg_DECL_temporary(ureg);
+
+   ureg_TEX(ureg, temp, tex_target, coord, sampler);
+   ureg_MAD(ureg, ureg_writemask(temp, TGSI_WRITEMASK_XYZ),
+            ureg_src(temp),
+            ureg_scalar(imm, TGSI_SWIZZLE_X),
+            ureg_scalar(imm, TGSI_SWIZZLE_Y));
+   ureg_MOV(ureg, out, ureg_src(temp));
+   ureg_END(ureg);
+
+   return ureg_create_shader_and_destroy(ureg, pipe);
+}
+
+
 /**
  * Make simple fragment texture shader:
  *  IMM {0,0,0,1}                         // (if writemask != 0xf)
@@ -213,17 +278,19 @@ void *util_make_layered_clear_geometry_shader(struct pipe_context *pipe)
  *  MOV OUT[0], TEMP[0]
  *  END;
  *
- * \param tex_target  one of PIPE_TEXTURE_x
+ * \param tex_target  one of TGSI_TEXTURE_x
  * \parma interp_mode  either TGSI_INTERPOLATE_LINEAR or PERSPECTIVE
  * \param writemask  mask of TGSI_WRITEMASK_x
  */
 void *
 util_make_fragment_tex_shader_writemask(struct pipe_context *pipe,
-                                        unsigned tex_target,
-                                        unsigned interp_mode,
+                                        enum tgsi_texture_type tex_target,
+                                        enum tgsi_interpolate_mode interp_mode,
                                         unsigned writemask,
                                         enum tgsi_return_type stype,
-                                        enum tgsi_return_type dtype)
+                                        enum tgsi_return_type dtype,
+                                        bool load_level_zero,
+                                        bool use_txf)
 {
    struct ureg_program *ureg;
    struct ureg_src sampler;
@@ -256,7 +323,7 @@ util_make_fragment_tex_shader_writemask(struct pipe_context *pipe,
    if (writemask != TGSI_WRITEMASK_XYZW) {
       struct ureg_src imm = ureg_imm4f( ureg, 0, 0, 0, 1 );
 
-      ureg_MOV( ureg, out, imm );
+      ureg_MOV(ureg, temp, imm);
    }
 
    if (tex_target == TGSI_TEXTURE_BUFFER)
@@ -264,9 +331,8 @@ util_make_fragment_tex_shader_writemask(struct pipe_context *pipe,
                ureg_writemask(temp, writemask),
                tex_target, tex, sampler);
    else
-      ureg_TEX(ureg,
-               ureg_writemask(temp, writemask),
-               tex_target, tex, sampler);
+      ureg_load_tex(ureg, ureg_writemask(temp, writemask), tex, sampler,
+                    tex_target, load_level_zero, use_txf);
 
    if (stype != dtype) {
       if (stype == TGSI_RETURN_TYPE_SINT) {
@@ -292,19 +358,23 @@ util_make_fragment_tex_shader_writemask(struct pipe_context *pipe,
 /**
  * Make a simple fragment shader that sets the output color to a color
  * taken from a texture.
- * \param tex_target  one of PIPE_TEXTURE_x
+ * \param tex_target  one of TGSI_TEXTURE_x
  */
 void *
-util_make_fragment_tex_shader(struct pipe_context *pipe, unsigned tex_target,
-                              unsigned interp_mode,
+util_make_fragment_tex_shader(struct pipe_context *pipe,
+                              enum tgsi_texture_type tex_target,
+                              enum tgsi_interpolate_mode interp_mode,
                               enum tgsi_return_type stype,
-                              enum tgsi_return_type dtype)
+                              enum tgsi_return_type dtype,
+                              bool load_level_zero,
+                              bool use_txf)
 {
    return util_make_fragment_tex_shader_writemask( pipe,
                                                    tex_target,
                                                    interp_mode,
                                                    TGSI_WRITEMASK_XYZW,
-                                                   stype, dtype );
+                                                   stype, dtype, load_level_zero,
+                                                   use_txf);
 }
 
 
@@ -314,8 +384,10 @@ util_make_fragment_tex_shader(struct pipe_context *pipe, unsigned tex_target,
  */
 void *
 util_make_fragment_tex_shader_writedepth(struct pipe_context *pipe,
-                                         unsigned tex_target,
-                                         unsigned interp_mode)
+                                         enum tgsi_texture_type tex_target,
+                                         enum tgsi_interpolate_mode interp_mode,
+                                         bool load_level_zero,
+                                         bool use_txf)
 {
    struct ureg_program *ureg;
    struct ureg_src sampler;
@@ -351,9 +423,8 @@ util_make_fragment_tex_shader_writedepth(struct pipe_context *pipe,
 
    ureg_MOV( ureg, out, imm );
 
-   ureg_TEX( ureg,
-             ureg_writemask(depth, TGSI_WRITEMASK_Z),
-             tex_target, tex, sampler );
+   ureg_load_tex(ureg, ureg_writemask(depth, TGSI_WRITEMASK_Z), tex, sampler,
+                 tex_target, load_level_zero, use_txf);
    ureg_END( ureg );
 
    return ureg_create_shader_and_destroy( ureg, pipe );
@@ -366,8 +437,10 @@ util_make_fragment_tex_shader_writedepth(struct pipe_context *pipe,
  */
 void *
 util_make_fragment_tex_shader_writedepthstencil(struct pipe_context *pipe,
-                                                unsigned tex_target,
-                                                unsigned interp_mode)
+                                         enum tgsi_texture_type tex_target,
+                                         enum tgsi_interpolate_mode interp_mode,
+                                         bool load_level_zero,
+                                         bool use_txf)
 {
    struct ureg_program *ureg;
    struct ureg_src depth_sampler, stencil_sampler;
@@ -412,12 +485,10 @@ util_make_fragment_tex_shader_writedepthstencil(struct pipe_context *pipe,
 
    ureg_MOV( ureg, out, imm );
 
-   ureg_TEX( ureg,
-             ureg_writemask(depth, TGSI_WRITEMASK_Z),
-             tex_target, tex, depth_sampler );
-   ureg_TEX( ureg,
-             ureg_writemask(stencil, TGSI_WRITEMASK_Y),
-             tex_target, tex, stencil_sampler );
+   ureg_load_tex(ureg, ureg_writemask(depth, TGSI_WRITEMASK_Z), tex,
+                 depth_sampler, tex_target, load_level_zero, use_txf);
+   ureg_load_tex(ureg, ureg_writemask(stencil, TGSI_WRITEMASK_Y), tex,
+                 stencil_sampler, tex_target, load_level_zero, use_txf);
    ureg_END( ureg );
 
    return ureg_create_shader_and_destroy( ureg, pipe );
@@ -430,8 +501,10 @@ util_make_fragment_tex_shader_writedepthstencil(struct pipe_context *pipe,
  */
 void *
 util_make_fragment_tex_shader_writestencil(struct pipe_context *pipe,
-                                           unsigned tex_target,
-                                           unsigned interp_mode)
+                                         enum tgsi_texture_type tex_target,
+                                         enum tgsi_interpolate_mode interp_mode,
+                                         bool load_level_zero,
+                                         bool use_txf)
 {
    struct ureg_program *ureg;
    struct ureg_src stencil_sampler;
@@ -467,9 +540,8 @@ util_make_fragment_tex_shader_writestencil(struct pipe_context *pipe,
 
    ureg_MOV( ureg, out, imm );
 
-   ureg_TEX( ureg,
-             ureg_writemask(stencil, TGSI_WRITEMASK_Y),
-             tex_target, tex, stencil_sampler );
+   ureg_load_tex(ureg, ureg_writemask(stencil, TGSI_WRITEMASK_Y), tex,
+                 stencil_sampler, tex_target, load_level_zero, use_txf);
    ureg_END( ureg );
 
    return ureg_create_shader_and_destroy( ureg, pipe );
@@ -565,7 +637,7 @@ util_make_fragment_cloneinput_shader(struct pipe_context *pipe, int num_cbufs,
 
 static void *
 util_make_fs_blit_msaa_gen(struct pipe_context *pipe,
-                           unsigned tgsi_tex,
+                           enum tgsi_texture_type tgsi_tex,
                            const char *samp_type,
                            const char *output_semantic,
                            const char *output_mask,
@@ -595,8 +667,8 @@ util_make_fs_blit_msaa_gen(struct pipe_context *pipe,
    assert(tgsi_tex == TGSI_TEXTURE_2D_MSAA ||
           tgsi_tex == TGSI_TEXTURE_2D_ARRAY_MSAA);
 
-   snprintf(text, sizeof(text), shader_templ, type, samp_type,
-            output_semantic, conversion_decl, type, conversion, output_mask);
+   util_snprintf(text, sizeof(text), shader_templ, type, samp_type,
+                 output_semantic, conversion_decl, type, conversion, output_mask);
 
    if (!tgsi_text_translate(text, tokens, ARRAY_SIZE(tokens))) {
       puts(text);
@@ -619,7 +691,7 @@ util_make_fs_blit_msaa_gen(struct pipe_context *pipe,
  */
 void *
 util_make_fs_blit_msaa_color(struct pipe_context *pipe,
-                             unsigned tgsi_tex,
+                             enum tgsi_texture_type tgsi_tex,
                              enum tgsi_return_type stype,
                              enum tgsi_return_type dtype)
 {
@@ -632,14 +704,14 @@ util_make_fs_blit_msaa_color(struct pipe_context *pipe,
 
       if (dtype == TGSI_RETURN_TYPE_SINT) {
          conversion_decl = "IMM[0] UINT32 {2147483647, 0, 0, 0}\n";
-         conversion = "UMIN TEMP[0], TEMP[0], IMM[0].x\n";
+         conversion = "UMIN TEMP[0], TEMP[0], IMM[0].xxxx\n";
       }
    } else if (stype == TGSI_RETURN_TYPE_SINT) {
       samp_type = "SINT";
 
       if (dtype == TGSI_RETURN_TYPE_UINT) {
          conversion_decl = "IMM[0] INT32 {0, 0, 0, 0}\n";
-         conversion = "IMAX TEMP[0], TEMP[0], IMM[0].x\n";
+         conversion = "IMAX TEMP[0], TEMP[0], IMM[0].xxxx\n";
       }
    } else {
       assert(dtype == TGSI_RETURN_TYPE_FLOAT);
@@ -659,7 +731,7 @@ util_make_fs_blit_msaa_color(struct pipe_context *pipe,
  */
 void *
 util_make_fs_blit_msaa_depth(struct pipe_context *pipe,
-                             unsigned tgsi_tex)
+                             enum tgsi_texture_type tgsi_tex)
 {
    return util_make_fs_blit_msaa_gen(pipe, tgsi_tex, "FLOAT",
                                      "POSITION", ".z", "", "");
@@ -673,7 +745,7 @@ util_make_fs_blit_msaa_depth(struct pipe_context *pipe,
  */
 void *
 util_make_fs_blit_msaa_stencil(struct pipe_context *pipe,
-                               unsigned tgsi_tex)
+                               enum tgsi_texture_type tgsi_tex)
 {
    return util_make_fs_blit_msaa_gen(pipe, tgsi_tex, "UINT",
                                      "STENCIL", ".y", "", "");
@@ -689,7 +761,7 @@ util_make_fs_blit_msaa_stencil(struct pipe_context *pipe,
  */
 void *
 util_make_fs_blit_msaa_depthstencil(struct pipe_context *pipe,
-                                    unsigned tgsi_tex)
+                                    enum tgsi_texture_type tgsi_tex)
 {
    static const char shader_templ[] =
          "FRAG\n"
@@ -730,7 +802,7 @@ util_make_fs_blit_msaa_depthstencil(struct pipe_context *pipe,
 
 void *
 util_make_fs_msaa_resolve(struct pipe_context *pipe,
-                          unsigned tgsi_tex, unsigned nr_samples,
+                          enum tgsi_texture_type tgsi_tex, unsigned nr_samples,
                           enum tgsi_return_type stype)
 {
    struct ureg_program *ureg;
@@ -790,7 +862,8 @@ util_make_fs_msaa_resolve(struct pipe_context *pipe,
 
 void *
 util_make_fs_msaa_resolve_bilinear(struct pipe_context *pipe,
-                                   unsigned tgsi_tex, unsigned nr_samples,
+                                   enum tgsi_texture_type tgsi_tex,
+                                   unsigned nr_samples,
                                    enum tgsi_return_type stype)
 {
    struct ureg_program *ureg;
@@ -925,7 +998,7 @@ util_make_geometry_passthrough_shader(struct pipe_context *pipe,
    }
 
    /* EMIT IMM[0] */
-   ureg_insn(ureg, TGSI_OPCODE_EMIT, NULL, 0, &imm, 1);
+   ureg_insn(ureg, TGSI_OPCODE_EMIT, NULL, 0, &imm, 1, 0);
 
    /* END */
    ureg_END(ureg);