+/*
+ * seamless cubemap neighbour array.
+ * this array is used to find the adjacent face in each of 4 directions,
+ * left, right, up, down. (or -x, +x, -y, +y).
+ */
+static const unsigned face_array[PIPE_TEX_FACE_MAX][4] = {
+ /* pos X first then neg X is Z different, Y the same */
+ /* PIPE_TEX_FACE_POS_X,*/
+ { PIPE_TEX_FACE_POS_Z, PIPE_TEX_FACE_NEG_Z,
+ PIPE_TEX_FACE_NEG_Y, PIPE_TEX_FACE_POS_Y },
+ /* PIPE_TEX_FACE_NEG_X */
+ { PIPE_TEX_FACE_NEG_Z, PIPE_TEX_FACE_POS_Z,
+ PIPE_TEX_FACE_NEG_Y, PIPE_TEX_FACE_POS_Y },
+
+ /* pos Y first then neg Y is X different, X the same */
+ /* PIPE_TEX_FACE_POS_Y */
+ { PIPE_TEX_FACE_NEG_X, PIPE_TEX_FACE_POS_X,
+ PIPE_TEX_FACE_POS_Z, PIPE_TEX_FACE_NEG_Z },
+
+ /* PIPE_TEX_FACE_NEG_Y */
+ { PIPE_TEX_FACE_NEG_X, PIPE_TEX_FACE_POS_X,
+ PIPE_TEX_FACE_NEG_Z, PIPE_TEX_FACE_POS_Z },
+
+ /* pos Z first then neg Y is X different, X the same */
+ /* PIPE_TEX_FACE_POS_Z */
+ { PIPE_TEX_FACE_NEG_X, PIPE_TEX_FACE_POS_X,
+ PIPE_TEX_FACE_NEG_Y, PIPE_TEX_FACE_POS_Y },
+
+ /* PIPE_TEX_FACE_NEG_Z */
+ { PIPE_TEX_FACE_POS_X, PIPE_TEX_FACE_NEG_X,
+ PIPE_TEX_FACE_NEG_Y, PIPE_TEX_FACE_POS_Y }
+};
+
+static INLINE unsigned
+get_next_face(unsigned face, int x, int y)
+{
+ int idx = 0;
+
+ if (x == 0 && y == 0)
+ return face;
+ if (x == -1)
+ idx = 0;
+ else if (x == 1)
+ idx = 1;
+ else if (y == -1)
+ idx = 2;
+ else if (y == 1)
+ idx = 3;
+
+ return face_array[face][idx];
+}
+
+static INLINE const float *
+get_texel_cube_seamless(const struct sp_sampler_view *sp_sview,
+ union tex_tile_address addr, int x, int y,
+ float *corner)
+{
+ const struct pipe_resource *texture = sp_sview->base.texture;
+ unsigned level = addr.bits.level;
+ unsigned face = addr.bits.face;
+ int new_x, new_y;
+ int max_x, max_y;
+ int c;
+
+ max_x = (int) u_minify(texture->width0, level);
+ max_y = (int) u_minify(texture->height0, level);
+ new_x = x;
+ new_y = y;
+
+ /* the corner case */
+ if ((x < 0 || x >= max_x) &&
+ (y < 0 || y >= max_y)) {
+ const float *c1, *c2, *c3;
+ int fx = x < 0 ? 0 : max_x - 1;
+ int fy = y < 0 ? 0 : max_y - 1;
+ c1 = get_texel_2d_no_border( sp_sview, addr, fx, fy);
+ addr.bits.face = get_next_face(face, (x < 0) ? -1 : 1, 0);
+ c2 = get_texel_2d_no_border( sp_sview, addr, (x < 0) ? max_x - 1 : 0, fy);
+ addr.bits.face = get_next_face(face, 0, (y < 0) ? -1 : 1);
+ c3 = get_texel_2d_no_border( sp_sview, addr, fx, (y < 0) ? max_y - 1 : 0);
+ for (c = 0; c < TGSI_QUAD_SIZE; c++)
+ corner[c] = CLAMP((c1[c] + c2[c] + c3[c]), 0.0F, 1.0F) / 3;
+
+ return corner;
+ }
+ /* change the face */
+ if (x < 0) {
+ new_x = max_x - 1;
+ face = get_next_face(face, -1, 0);
+ } else if (x >= max_x) {
+ new_x = 0;
+ face = get_next_face(face, 1, 0);
+ } else if (y < 0) {
+ new_y = max_y - 1;
+ face = get_next_face(face, 0, -1);
+ } else if (y >= max_y) {
+ new_y = 0;
+ face = get_next_face(face, 0, 1);
+ }
+
+ addr.bits.face = face;
+ return get_texel_2d_no_border( sp_sview, addr, new_x, new_y );
+}