freedreno/ir3: introduce ir3_compiler object
[mesa.git] / src / gallium / drivers / freedreno / a4xx / fd4_texture.c
index fc9c8735815096d2d42ea22bb7416341ab94fced..ff1ff8f0d3433f0fd5a8aa615ed2ec7112fdf204 100644 (file)
@@ -33,7 +33,7 @@
 #include "util/u_format.h"
 
 #include "fd4_texture.h"
-#include "fd4_util.h"
+#include "fd4_format.h"
 
 /* TODO do we need to emulate clamp-to-edge like a3xx? */
 static enum a4xx_tex_clamp
@@ -68,13 +68,13 @@ tex_clamp(unsigned wrap)
 }
 
 static enum a4xx_tex_filter
-tex_filter(unsigned filter)
+tex_filter(unsigned filter, bool aniso)
 {
        switch (filter) {
        case PIPE_TEX_FILTER_NEAREST:
                return A4XX_TEX_NEAREST;
        case PIPE_TEX_FILTER_LINEAR:
-               return A4XX_TEX_LINEAR;
+               return aniso ? A4XX_TEX_ANISO : A4XX_TEX_LINEAR;
        default:
                DBG("invalid filter: %u", filter);
                return 0;
@@ -86,25 +86,34 @@ fd4_sampler_state_create(struct pipe_context *pctx,
                const struct pipe_sampler_state *cso)
 {
        struct fd4_sampler_stateobj *so = CALLOC_STRUCT(fd4_sampler_stateobj);
+       unsigned aniso = util_last_bit(MIN2(cso->max_anisotropy >> 1, 8));
+       bool miplinear = false;
 
        if (!so)
                return NULL;
 
+       if (cso->min_mip_filter == PIPE_TEX_MIPFILTER_LINEAR)
+               miplinear = true;
+
        so->base = *cso;
 
        so->texsamp0 =
-               A4XX_TEX_SAMP_0_XY_MAG(tex_filter(cso->mag_img_filter)) |
-               A4XX_TEX_SAMP_0_XY_MIN(tex_filter(cso->min_img_filter)) |
+               COND(miplinear, A4XX_TEX_SAMP_0_MIPFILTER_LINEAR_NEAR) |
+               A4XX_TEX_SAMP_0_XY_MAG(tex_filter(cso->mag_img_filter, aniso)) |
+               A4XX_TEX_SAMP_0_XY_MIN(tex_filter(cso->min_img_filter, aniso)) |
+               A4XX_TEX_SAMP_0_ANISO(aniso) |
                A4XX_TEX_SAMP_0_WRAP_S(tex_clamp(cso->wrap_s)) |
                A4XX_TEX_SAMP_0_WRAP_T(tex_clamp(cso->wrap_t)) |
                A4XX_TEX_SAMP_0_WRAP_R(tex_clamp(cso->wrap_r));
 
+       so->texsamp1 =
+//             COND(miplinear, A4XX_TEX_SAMP_1_MIPFILTER_LINEAR_FAR) |
+               COND(!cso->normalized_coords, A4XX_TEX_SAMP_1_UNNORM_COORDS);
+
        if (cso->min_mip_filter != PIPE_TEX_MIPFILTER_NONE) {
-               so->texsamp1 =
+               so->texsamp1 |=
                        A4XX_TEX_SAMP_1_MIN_LOD(cso->min_lod) |
                        A4XX_TEX_SAMP_1_MAX_LOD(cso->max_lod);
-       } else {
-               so->texsamp1 = 0x00000000;
        }
 
        if (cso->compare_mode)
@@ -142,6 +151,7 @@ fd4_sampler_view_create(struct pipe_context *pctx, struct pipe_resource *prsc,
        struct fd4_pipe_sampler_view *so = CALLOC_STRUCT(fd4_pipe_sampler_view);
        struct fd_resource *rsc = fd_resource(prsc);
        unsigned lvl = cso->u.tex.first_level;
+       unsigned miplevels = cso->u.tex.last_level - lvl;
 
        if (!so)
                return NULL;
@@ -152,25 +162,39 @@ fd4_sampler_view_create(struct pipe_context *pctx, struct pipe_resource *prsc,
        so->base.reference.count = 1;
        so->base.context = pctx;
 
-       so->tex_resource =  rsc;
-
        so->texconst0 =
                A4XX_TEX_CONST_0_TYPE(tex_type(prsc->target)) |
                A4XX_TEX_CONST_0_FMT(fd4_pipe2tex(cso->format)) |
+               A4XX_TEX_CONST_0_MIPLVLS(miplevels) |
                fd4_tex_swiz(cso->format, cso->swizzle_r, cso->swizzle_g,
                                cso->swizzle_b, cso->swizzle_a);
 
+       if (util_format_is_srgb(cso->format))
+               so->texconst0 |= A4XX_TEX_CONST_0_SRGB;
+
        so->texconst1 =
-               A4XX_TEX_CONST_1_WIDTH(prsc->width0) |
-               A4XX_TEX_CONST_1_HEIGHT(prsc->height0);
+               A4XX_TEX_CONST_1_WIDTH(u_minify(prsc->width0, lvl)) |
+               A4XX_TEX_CONST_1_HEIGHT(u_minify(prsc->height0, lvl));
        so->texconst2 =
+               A4XX_TEX_CONST_2_FETCHSIZE(fd4_pipe2fetchsize(cso->format)) |
                A4XX_TEX_CONST_2_PITCH(rsc->slices[lvl].pitch * rsc->cpp);
 
        switch (prsc->target) {
        case PIPE_TEXTURE_1D_ARRAY:
        case PIPE_TEXTURE_2D_ARRAY:
+               so->texconst3 =
+                       A4XX_TEX_CONST_3_DEPTH(prsc->array_size) |
+                       A4XX_TEX_CONST_3_LAYERSZ(rsc->layer_size);
+               break;
+       case PIPE_TEXTURE_CUBE:
+       case PIPE_TEXTURE_CUBE_ARRAY:  /* ?? not sure about _CUBE_ARRAY */
+               so->texconst3 =
+                       A4XX_TEX_CONST_3_DEPTH(1) |
+                       A4XX_TEX_CONST_3_LAYERSZ(rsc->layer_size);
+               break;
        case PIPE_TEXTURE_3D:
                so->texconst3 =
+                       A4XX_TEX_CONST_3_DEPTH(u_minify(prsc->depth0, lvl)) |
                        A4XX_TEX_CONST_3_LAYERSZ(rsc->slices[0].size0);
                break;
        default:
@@ -181,10 +205,43 @@ fd4_sampler_view_create(struct pipe_context *pctx, struct pipe_resource *prsc,
        return &so->base;
 }
 
+static void
+fd4_set_sampler_views(struct pipe_context *pctx, unsigned shader,
+               unsigned start, unsigned nr, struct pipe_sampler_view **views)
+{
+       struct fd_context *ctx = fd_context(pctx);
+       struct fd4_context *fd4_ctx = fd4_context(ctx);
+       struct fd_texture_stateobj *tex;
+       uint16_t integer_s = 0, *ptr;
+       int i;
+
+       fd_set_sampler_views(pctx, shader, start, nr, views);
+
+       switch (shader) {
+       case PIPE_SHADER_FRAGMENT:
+               tex = &ctx->fragtex;
+               ptr = &fd4_ctx->finteger_s;
+               break;
+       case PIPE_SHADER_VERTEX:
+               tex = &ctx->verttex;
+               ptr = &fd4_ctx->vinteger_s;
+               break;
+       default:
+               return;
+       }
+
+       for (i = 0; i < tex->num_textures; i++)
+               if (util_format_is_pure_integer(tex->textures[i]->format))
+                       integer_s |= 1 << i;
+
+       *ptr = integer_s;
+}
+
 void
 fd4_texture_init(struct pipe_context *pctx)
 {
        pctx->create_sampler_state = fd4_sampler_state_create;
        pctx->bind_sampler_states = fd_sampler_states_bind;
        pctx->create_sampler_view = fd4_sampler_view_create;
+       pctx->set_sampler_views = fd4_set_sampler_views;
 }