+static void *
+util_make_fs_blit_msaa_gen(struct pipe_context *pipe,
+ unsigned tgsi_tex,
+ const char *samp_type,
+ const char *output_semantic,
+ const char *output_mask,
+ const char *conversion_decl,
+ const char *conversion)
+{
+ static const char shader_templ[] =
+ "FRAG\n"
+ "DCL IN[0], GENERIC[0], LINEAR\n"
+ "DCL SAMP[0]\n"
+ "DCL SVIEW[0], %s, %s\n"
+ "DCL OUT[0], %s\n"
+ "DCL TEMP[0]\n"
+ "%s"
+
+ "F2U TEMP[0], IN[0]\n"
+ "TXF TEMP[0], TEMP[0], SAMP[0], %s\n"
+ "%s"
+ "MOV OUT[0]%s, TEMP[0]\n"
+ "END\n";
+
+ const char *type = tgsi_texture_names[tgsi_tex];
+ char text[sizeof(shader_templ)+100];
+ struct tgsi_token tokens[1000];
+ struct pipe_shader_state state;
+
+ 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);
+
+ if (!tgsi_text_translate(text, tokens, ARRAY_SIZE(tokens))) {
+ puts(text);
+ assert(0);
+ return NULL;
+ }
+ pipe_shader_state_from_tgsi(&state, tokens);
+#if 0
+ tgsi_dump(state.tokens, 0);
+#endif
+
+ return pipe->create_fs_state(pipe, &state);
+}
+
+
+/**
+ * Make a fragment shader that sets the output color to a color
+ * fetched from a multisample texture.
+ * \param tex_target one of PIPE_TEXTURE_x
+ */
+void *
+util_make_fs_blit_msaa_color(struct pipe_context *pipe,
+ unsigned tgsi_tex,
+ enum tgsi_return_type stype,
+ enum tgsi_return_type dtype)
+{
+ const char *samp_type;
+ const char *conversion_decl = "";
+ const char *conversion = "";
+
+ if (stype == TGSI_RETURN_TYPE_UINT) {
+ samp_type = "UINT";
+
+ 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";
+ }
+ } 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";
+ }
+ } else {
+ assert(dtype == TGSI_RETURN_TYPE_FLOAT);
+ samp_type = "FLOAT";
+ }
+
+ return util_make_fs_blit_msaa_gen(pipe, tgsi_tex, samp_type,
+ "COLOR[0]", "", conversion_decl,
+ conversion);
+}
+
+
+/**
+ * Make a fragment shader that sets the output depth to a depth value
+ * fetched from a multisample texture.
+ * \param tex_target one of PIPE_TEXTURE_x
+ */
+void *
+util_make_fs_blit_msaa_depth(struct pipe_context *pipe,
+ unsigned tgsi_tex)
+{
+ return util_make_fs_blit_msaa_gen(pipe, tgsi_tex, "FLOAT",
+ "POSITION", ".z", "", "");
+}
+
+
+/**
+ * Make a fragment shader that sets the output stencil to a stencil value
+ * fetched from a multisample texture.
+ * \param tex_target one of PIPE_TEXTURE_x
+ */
+void *
+util_make_fs_blit_msaa_stencil(struct pipe_context *pipe,
+ unsigned tgsi_tex)
+{
+ return util_make_fs_blit_msaa_gen(pipe, tgsi_tex, "UINT",
+ "STENCIL", ".y", "", "");
+}
+
+
+/**
+ * Make a fragment shader that sets the output depth and stencil to depth
+ * and stencil values fetched from two multisample textures / samplers.
+ * The sizes of both textures should match (it should be one depth-stencil
+ * texture).
+ * \param tex_target one of PIPE_TEXTURE_x
+ */
+void *
+util_make_fs_blit_msaa_depthstencil(struct pipe_context *pipe,
+ unsigned tgsi_tex)
+{
+ static const char shader_templ[] =
+ "FRAG\n"
+ "DCL IN[0], GENERIC[0], LINEAR\n"
+ "DCL SAMP[0..1]\n"
+ "DCL SVIEW[0..1], %s, FLOAT\n"
+ "DCL OUT[0], POSITION\n"
+ "DCL OUT[1], STENCIL\n"
+ "DCL TEMP[0]\n"
+
+ "F2U TEMP[0], IN[0]\n"
+ "TXF OUT[0].z, TEMP[0], SAMP[0], %s\n"
+ "TXF OUT[1].y, TEMP[0], SAMP[1], %s\n"
+ "END\n";
+
+ const char *type = tgsi_texture_names[tgsi_tex];
+ char text[sizeof(shader_templ)+100];
+ struct tgsi_token tokens[1000];
+ struct pipe_shader_state state;
+
+ assert(tgsi_tex == TGSI_TEXTURE_2D_MSAA ||
+ tgsi_tex == TGSI_TEXTURE_2D_ARRAY_MSAA);
+
+ sprintf(text, shader_templ, type, type, type);
+
+ if (!tgsi_text_translate(text, tokens, ARRAY_SIZE(tokens))) {
+ assert(0);
+ return NULL;
+ }
+ pipe_shader_state_from_tgsi(&state, tokens);
+#if 0
+ tgsi_dump(state.tokens, 0);
+#endif
+
+ return pipe->create_fs_state(pipe, &state);
+}
+
+
+void *
+util_make_fs_msaa_resolve(struct pipe_context *pipe,
+ unsigned tgsi_tex, unsigned nr_samples,
+ enum tgsi_return_type stype)
+{
+ struct ureg_program *ureg;
+ struct ureg_src sampler, coord;
+ struct ureg_dst out, tmp_sum, tmp_coord, tmp;
+ unsigned i;
+
+ ureg = ureg_create(PIPE_SHADER_FRAGMENT);
+ if (!ureg)
+ return NULL;
+
+ /* Declarations. */
+ sampler = ureg_DECL_sampler(ureg, 0);
+ ureg_DECL_sampler_view(ureg, 0, tgsi_tex, 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);
+ tmp_sum = ureg_DECL_temporary(ureg);
+ tmp_coord = ureg_DECL_temporary(ureg);
+ tmp = ureg_DECL_temporary(ureg);
+
+ /* Instructions. */
+ ureg_MOV(ureg, tmp_sum, ureg_imm1f(ureg, 0));
+ ureg_F2U(ureg, tmp_coord, coord);
+
+ for (i = 0; i < nr_samples; i++) {
+ /* Read one sample. */
+ ureg_MOV(ureg, ureg_writemask(tmp_coord, TGSI_WRITEMASK_W),
+ ureg_imm1u(ureg, i));
+ ureg_TXF(ureg, tmp, tgsi_tex, ureg_src(tmp_coord), sampler);
+
+ if (stype == TGSI_RETURN_TYPE_UINT)
+ ureg_U2F(ureg, tmp, ureg_src(tmp));
+ else if (stype == TGSI_RETURN_TYPE_SINT)
+ ureg_I2F(ureg, tmp, ureg_src(tmp));
+
+ /* Add it to the sum.*/
+ ureg_ADD(ureg, tmp_sum, ureg_src(tmp_sum), ureg_src(tmp));
+ }
+
+ /* Calculate the average and return. */
+ ureg_MUL(ureg, tmp_sum, ureg_src(tmp_sum),
+ ureg_imm1f(ureg, 1.0 / nr_samples));
+
+ if (stype == TGSI_RETURN_TYPE_UINT)
+ ureg_F2U(ureg, out, ureg_src(tmp_sum));
+ else if (stype == TGSI_RETURN_TYPE_SINT)
+ ureg_F2I(ureg, out, ureg_src(tmp_sum));
+ else
+ ureg_MOV(ureg, out, ureg_src(tmp_sum));
+
+ ureg_END(ureg);
+
+ return ureg_create_shader_and_destroy(ureg, pipe);
+}
+
+
+void *
+util_make_fs_msaa_resolve_bilinear(struct pipe_context *pipe,
+ unsigned tgsi_tex, unsigned nr_samples,
+ enum tgsi_return_type stype)
+{
+ struct ureg_program *ureg;
+ struct ureg_src sampler, coord;
+ struct ureg_dst out, tmp, top, bottom;
+ struct ureg_dst tmp_coord[4], tmp_sum[4];
+ unsigned i, c;
+
+ ureg = ureg_create(PIPE_SHADER_FRAGMENT);
+ if (!ureg)
+ return NULL;
+
+ /* Declarations. */
+ sampler = ureg_DECL_sampler(ureg, 0);
+ ureg_DECL_sampler_view(ureg, 0, tgsi_tex, 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);
+ for (c = 0; c < 4; c++)
+ tmp_sum[c] = ureg_DECL_temporary(ureg);
+ for (c = 0; c < 4; c++)
+ tmp_coord[c] = ureg_DECL_temporary(ureg);
+ tmp = ureg_DECL_temporary(ureg);
+ top = ureg_DECL_temporary(ureg);
+ bottom = ureg_DECL_temporary(ureg);
+
+ /* Instructions. */
+ for (c = 0; c < 4; c++)
+ ureg_MOV(ureg, tmp_sum[c], ureg_imm1f(ureg, 0));
+
+ /* Get 4 texture coordinates for the bilinear filter. */
+ ureg_F2U(ureg, tmp_coord[0], coord); /* top-left */
+ ureg_UADD(ureg, tmp_coord[1], ureg_src(tmp_coord[0]),
+ ureg_imm4u(ureg, 1, 0, 0, 0)); /* top-right */
+ ureg_UADD(ureg, tmp_coord[2], ureg_src(tmp_coord[0]),
+ ureg_imm4u(ureg, 0, 1, 0, 0)); /* bottom-left */
+ ureg_UADD(ureg, tmp_coord[3], ureg_src(tmp_coord[0]),
+ ureg_imm4u(ureg, 1, 1, 0, 0)); /* bottom-right */
+
+ for (i = 0; i < nr_samples; i++) {
+ for (c = 0; c < 4; c++) {
+ /* Read one sample. */
+ ureg_MOV(ureg, ureg_writemask(tmp_coord[c], TGSI_WRITEMASK_W),
+ ureg_imm1u(ureg, i));
+ ureg_TXF(ureg, tmp, tgsi_tex, ureg_src(tmp_coord[c]), sampler);
+
+ if (stype == TGSI_RETURN_TYPE_UINT)
+ ureg_U2F(ureg, tmp, ureg_src(tmp));
+ else if (stype == TGSI_RETURN_TYPE_SINT)
+ ureg_I2F(ureg, tmp, ureg_src(tmp));
+
+ /* Add it to the sum.*/
+ ureg_ADD(ureg, tmp_sum[c], ureg_src(tmp_sum[c]), ureg_src(tmp));
+ }
+ }
+
+ /* Calculate the average. */
+ for (c = 0; c < 4; c++)
+ ureg_MUL(ureg, tmp_sum[c], ureg_src(tmp_sum[c]),
+ ureg_imm1f(ureg, 1.0 / nr_samples));
+
+ /* Take the 4 average values and apply a standard bilinear filter. */
+ ureg_FRC(ureg, tmp, coord);
+
+ ureg_LRP(ureg, top,
+ ureg_scalar(ureg_src(tmp), 0),
+ ureg_src(tmp_sum[1]),
+ ureg_src(tmp_sum[0]));
+
+ ureg_LRP(ureg, bottom,
+ ureg_scalar(ureg_src(tmp), 0),
+ ureg_src(tmp_sum[3]),
+ ureg_src(tmp_sum[2]));
+
+ ureg_LRP(ureg, tmp,
+ ureg_scalar(ureg_src(tmp), 1),
+ ureg_src(bottom),
+ ureg_src(top));
+
+ /* Convert to the texture format and return. */
+ if (stype == TGSI_RETURN_TYPE_UINT)
+ ureg_F2U(ureg, out, ureg_src(tmp));
+ else if (stype == TGSI_RETURN_TYPE_SINT)
+ ureg_F2I(ureg, out, ureg_src(tmp));
+ else
+ ureg_MOV(ureg, out, ureg_src(tmp));
+
+ ureg_END(ureg);
+
+ return ureg_create_shader_and_destroy(ureg, pipe);
+}
+
+void *
+util_make_geometry_passthrough_shader(struct pipe_context *pipe,
+ uint num_attribs,
+ const ubyte *semantic_names,
+ const ubyte *semantic_indexes)
+{
+ static const unsigned zero[4] = {0, 0, 0, 0};
+
+ struct ureg_program *ureg;
+ struct ureg_dst dst[PIPE_MAX_SHADER_OUTPUTS];
+ struct ureg_src src[PIPE_MAX_SHADER_INPUTS];
+ struct ureg_src imm;
+
+ unsigned i;
+
+ ureg = ureg_create(PIPE_SHADER_GEOMETRY);
+ if (!ureg)
+ return NULL;
+
+ ureg_property(ureg, TGSI_PROPERTY_GS_INPUT_PRIM, PIPE_PRIM_POINTS);
+ ureg_property(ureg, TGSI_PROPERTY_GS_OUTPUT_PRIM, PIPE_PRIM_POINTS);
+ ureg_property(ureg, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES, 1);
+ ureg_property(ureg, TGSI_PROPERTY_GS_INVOCATIONS, 1);
+ imm = ureg_DECL_immediate_uint(ureg, zero, 4);
+
+ /**
+ * Loop over all the attribs and declare the corresponding
+ * declarations in the geometry shader
+ */
+ for (i = 0; i < num_attribs; i++) {
+ src[i] = ureg_DECL_input(ureg, semantic_names[i],
+ semantic_indexes[i], 0, 1);
+ src[i] = ureg_src_dimension(src[i], 0);
+ dst[i] = ureg_DECL_output(ureg, semantic_names[i], semantic_indexes[i]);
+ }
+
+ /* MOV dst[i] src[i] */
+ for (i = 0; i < num_attribs; i++) {
+ ureg_MOV(ureg, dst[i], src[i]);
+ }
+
+ /* EMIT IMM[0] */
+ ureg_insn(ureg, TGSI_OPCODE_EMIT, NULL, 0, &imm, 1);
+
+ /* END */
+ ureg_END(ureg);
+
+ return ureg_create_shader_and_destroy(ureg, pipe);
+}
+