+static void
+sample_get_dims(struct tgsi_sampler *tgsi_sampler, int level,
+ int dims[4])
+{
+ struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
+ const struct pipe_sampler_view *view = samp->view;
+ const struct pipe_resource *texture = view->texture;
+
+ /* undefined according to EXT_gpu_program */
+ level += view->u.tex.first_level;
+ if (level > view->u.tex.last_level)
+ return;
+
+ dims[0] = u_minify(texture->width0, level);
+
+ switch(texture->target) {
+ case PIPE_TEXTURE_1D_ARRAY:
+ dims[1] = texture->array_size;
+ /* fallthrough */
+ case PIPE_TEXTURE_1D:
+ return;
+ case PIPE_TEXTURE_2D_ARRAY:
+ dims[2] = texture->array_size;
+ /* fallthrough */
+ case PIPE_TEXTURE_2D:
+ case PIPE_TEXTURE_CUBE:
+ case PIPE_TEXTURE_RECT:
+ dims[1] = u_minify(texture->height0, level);
+ return;
+ case PIPE_TEXTURE_3D:
+ dims[1] = u_minify(texture->height0, level);
+ dims[2] = u_minify(texture->depth0, level);
+ return;
+ case PIPE_TEXTURE_CUBE_ARRAY:
+ dims[1] = u_minify(texture->height0, level);
+ dims[2] = texture->array_size / 6;
+ break;
+ case PIPE_BUFFER:
+ dims[0] /= util_format_get_blocksize(view->format);
+ return;
+ default:
+ assert(!"unexpected texture target in sample_get_dims()");
+ return;
+ }
+}
+
+/**
+ * This function is only used for getting unfiltered texels via the
+ * TXF opcode. The GL spec says that out-of-bounds texel fetches
+ * produce undefined results. Instead of crashing, lets just clamp
+ * coords to the texture image size.
+ */
+static void
+sample_get_texels(struct tgsi_sampler *tgsi_sampler,
+ const int v_i[TGSI_QUAD_SIZE],
+ const int v_j[TGSI_QUAD_SIZE],
+ const int v_k[TGSI_QUAD_SIZE],
+ const int lod[TGSI_QUAD_SIZE],
+ const int8_t offset[3],
+ float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])
+{
+ const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
+ union tex_tile_address addr;
+ const struct pipe_resource *texture = samp->view->texture;
+ int j, c;
+ const float *tx;
+ const bool need_swizzle = any_swizzle(samp->key);
+ int width, height, depth, layers;
+
+ addr.value = 0;
+ /* TODO write a better test for LOD */
+ addr.bits.level = lod[0];
+
+ width = u_minify(texture->width0, addr.bits.level);
+ height = u_minify(texture->height0, addr.bits.level);
+ depth = u_minify(texture->depth0, addr.bits.level);
+ layers = texture->array_size;
+
+ switch(texture->target) {
+ case PIPE_BUFFER:
+ case PIPE_TEXTURE_1D:
+ for (j = 0; j < TGSI_QUAD_SIZE; j++) {
+ int x = CLAMP(v_i[j] + offset[0], 0, width - 1);
+ tx = get_texel_2d(samp, addr, x, 0);
+ for (c = 0; c < 4; c++) {
+ rgba[c][j] = tx[c];
+ }
+ }
+ break;
+ case PIPE_TEXTURE_1D_ARRAY:
+ for (j = 0; j < TGSI_QUAD_SIZE; j++) {
+ int x = CLAMP(v_i[j] + offset[0], 0, width - 1);
+ int y = CLAMP(v_j[j], 0, layers - 1);
+ tx = get_texel_1d_array(samp, addr, x, y);
+ for (c = 0; c < 4; c++) {
+ rgba[c][j] = tx[c];
+ }
+ }
+ break;
+ case PIPE_TEXTURE_2D:
+ case PIPE_TEXTURE_RECT:
+ for (j = 0; j < TGSI_QUAD_SIZE; j++) {
+ int x = CLAMP(v_i[j] + offset[0], 0, width - 1);
+ int y = CLAMP(v_j[j] + offset[1], 0, height - 1);
+ tx = get_texel_2d(samp, addr, x, y);
+ for (c = 0; c < 4; c++) {
+ rgba[c][j] = tx[c];
+ }
+ }
+ break;
+ case PIPE_TEXTURE_2D_ARRAY:
+ for (j = 0; j < TGSI_QUAD_SIZE; j++) {
+ int x = CLAMP(v_i[j] + offset[0], 0, width - 1);
+ int y = CLAMP(v_j[j] + offset[1], 0, height - 1);
+ int layer = CLAMP(v_k[j], 0, layers - 1);
+ tx = get_texel_2d_array(samp, addr, x, y, layer);
+ for (c = 0; c < 4; c++) {
+ rgba[c][j] = tx[c];
+ }
+ }
+ break;
+ case PIPE_TEXTURE_3D:
+ for (j = 0; j < TGSI_QUAD_SIZE; j++) {
+ int x = CLAMP(v_i[j] + offset[0], 0, width - 1);
+ int y = CLAMP(v_j[j] + offset[1], 0, height - 1);
+ int z = CLAMP(v_k[j] + offset[2], 0, depth - 1);
+
+ tx = get_texel_3d(samp, addr, x, y, z);
+ for (c = 0; c < 4; c++) {
+ rgba[c][j] = tx[c];
+ }
+ }
+ break;
+ case PIPE_TEXTURE_CUBE: /* TXF can't work on CUBE according to spec */
+ default:
+ assert(!"Unknown or CUBE texture type in TXF processing\n");
+ break;
+ }
+
+ if (need_swizzle) {
+ float rgba_temp[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE];
+ memcpy(rgba_temp, rgba, sizeof(rgba_temp));
+ do_swizzling(samp, rgba_temp, rgba);
+ }
+}
+
+