freedreno/ir3: handle color clamp variant ourselves
[mesa.git] / src / gallium / drivers / freedreno / freedreno_texture.c
index 212e5063c600cc43b5de99c90bcd1a08a4d9b284..a301811995d4ce2128db3fe6747857e6fbd8f1dd 100644 (file)
@@ -49,7 +49,7 @@ fd_sampler_view_destroy(struct pipe_context *pctx,
        FREE(view);
 }
 
-static void bind_sampler_states(struct fd_texture_stateobj *prog,
+static void bind_sampler_states(struct fd_texture_stateobj *tex,
                unsigned nr, void **hwcso)
 {
        unsigned i;
@@ -58,19 +58,19 @@ static void bind_sampler_states(struct fd_texture_stateobj *prog,
        for (i = 0; i < nr; i++) {
                if (hwcso[i])
                        new_nr = i + 1;
-               prog->samplers[i] = hwcso[i];
-               prog->dirty_samplers |= (1 << i);
+               tex->samplers[i] = hwcso[i];
+               tex->dirty_samplers |= (1 << i);
        }
 
-       for (; i < prog->num_samplers; i++) {
-               prog->samplers[i] = NULL;
-               prog->dirty_samplers |= (1 << i);
+       for (; i < tex->num_samplers; i++) {
+               tex->samplers[i] = NULL;
+               tex->dirty_samplers |= (1 << i);
        }
 
-       prog->num_samplers = new_nr;
+       tex->num_samplers = new_nr;
 }
 
-static void set_sampler_views(struct fd_texture_stateobj *prog,
+static void set_sampler_views(struct fd_texture_stateobj *tex,
                unsigned nr, struct pipe_sampler_view **views)
 {
        unsigned i;
@@ -79,19 +79,19 @@ static void set_sampler_views(struct fd_texture_stateobj *prog,
        for (i = 0; i < nr; i++) {
                if (views[i])
                        new_nr = i + 1;
-               pipe_sampler_view_reference(&prog->textures[i], views[i]);
-               prog->dirty_samplers |= (1 << i);
+               pipe_sampler_view_reference(&tex->textures[i], views[i]);
+               tex->dirty_samplers |= (1 << i);
        }
 
-       for (; i < prog->num_textures; i++) {
-               pipe_sampler_view_reference(&prog->textures[i], NULL);
-               prog->dirty_samplers |= (1 << i);
+       for (; i < tex->num_textures; i++) {
+               pipe_sampler_view_reference(&tex->textures[i], NULL);
+               tex->dirty_samplers |= (1 << i);
        }
 
-       prog->num_textures = new_nr;
+       tex->num_textures = new_nr;
 }
 
-static void
+void
 fd_sampler_states_bind(struct pipe_context *pctx,
                unsigned shader, unsigned start,
                unsigned nr, void **hwcso)
@@ -101,13 +101,6 @@ fd_sampler_states_bind(struct pipe_context *pctx,
        assert(start == 0);
 
        if (shader == PIPE_SHADER_FRAGMENT) {
-               /* on a2xx, since there is a flat address space for textures/samplers,
-                * a change in # of fragment textures/samplers will trigger patching and
-                * re-emitting the vertex shader:
-                */
-               if (nr != ctx->fragtex.num_samplers)
-                       ctx->dirty |= FD_DIRTY_TEXSTATE;
-
                bind_sampler_states(&ctx->fragtex, nr, hwcso);
                ctx->dirty |= FD_DIRTY_FRAGTEX;
        }
@@ -117,49 +110,36 @@ fd_sampler_states_bind(struct pipe_context *pctx,
        }
 }
 
-
-static void
-fd_fragtex_set_sampler_views(struct pipe_context *pctx, unsigned nr,
+void
+fd_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);
 
-       /* on a2xx, since there is a flat address space for textures/samplers,
-        * a change in # of fragment textures/samplers will trigger patching and
-        * re-emitting the vertex shader:
-        */
-       if (nr != ctx->fragtex.num_textures)
-               ctx->dirty |= FD_DIRTY_TEXSTATE;
-
-       set_sampler_views(&ctx->fragtex, nr, views);
-       ctx->dirty |= FD_DIRTY_FRAGTEX;
-}
+       assert(start == 0);
 
-static void
-fd_verttex_set_sampler_views(struct pipe_context *pctx, unsigned nr,
-               struct pipe_sampler_view **views)
-{
-       struct fd_context *ctx = fd_context(pctx);
-       set_sampler_views(&ctx->verttex, nr, views);
-       ctx->dirty |= FD_DIRTY_VERTTEX;
-}
+       switch (shader) {
+       case PIPE_SHADER_FRAGMENT:
+               /* on a2xx, since there is a flat address space for textures/samplers,
+                * a change in # of fragment textures/samplers will trigger patching
+                * and re-emitting the vertex shader:
+                *
+                * (note: later gen's ignore FD_DIRTY_TEXSTATE so fine to set it)
+                */
+               if (nr != ctx->fragtex.num_textures)
+                       ctx->dirty |= FD_DIRTY_TEXSTATE;
 
-static void
-fd_set_sampler_views(struct pipe_context *pctx, unsigned shader,
-                     unsigned start, unsigned nr,
-                     struct pipe_sampler_view **views)
-{
-   assert(start == 0);
-   switch (shader) {
-   case PIPE_SHADER_FRAGMENT:
-      fd_fragtex_set_sampler_views(pctx, nr, views);
-      break;
-   case PIPE_SHADER_VERTEX:
-      fd_verttex_set_sampler_views(pctx, nr, views);
-      break;
-   default:
-      ;
-   }
+               set_sampler_views(&ctx->fragtex, nr, views);
+               ctx->dirty |= FD_DIRTY_FRAGTEX;
+               break;
+       case PIPE_SHADER_VERTEX:
+               set_sampler_views(&ctx->verttex, nr, views);
+               ctx->dirty |= FD_DIRTY_VERTTEX;
+               break;
+       default:
+               break;
+       }
 }
 
 void
@@ -168,7 +148,52 @@ fd_texture_init(struct pipe_context *pctx)
        pctx->delete_sampler_state = fd_sampler_state_delete;
 
        pctx->sampler_view_destroy = fd_sampler_view_destroy;
+}
 
-       pctx->bind_sampler_states = fd_sampler_states_bind;
-       pctx->set_sampler_views = fd_set_sampler_views;
+/* helper for setting up border-color buffer for a3xx/a4xx: */
+void
+fd_setup_border_colors(struct fd_texture_stateobj *tex, void *ptr,
+               unsigned offset)
+{
+       unsigned i, j;
+
+       for (i = 0; i < tex->num_samplers; i++) {
+               struct pipe_sampler_state *sampler = tex->samplers[i];
+               uint16_t *bcolor = (uint16_t *)((uint8_t *)ptr +
+                               (BORDERCOLOR_SIZE * offset) +
+                               (BORDERCOLOR_SIZE * i));
+               uint32_t *bcolor32 = (uint32_t *)&bcolor[16];
+
+               if (!sampler)
+                       continue;
+
+               /*
+                * XXX HACK ALERT XXX
+                *
+                * The border colors need to be swizzled in a particular
+                * format-dependent order. Even though samplers don't know about
+                * formats, we can assume that with a GL state tracker, there's a
+                * 1:1 correspondence between sampler and texture. Take advantage
+                * of that knowledge.
+                */
+               if (i < tex->num_textures && tex->textures[i]) {
+                       const struct util_format_description *desc =
+                                       util_format_description(tex->textures[i]->format);
+                       for (j = 0; j < 4; j++) {
+                               if (desc->swizzle[j] >= 4)
+                                       continue;
+
+                               const struct util_format_channel_description *chan =
+                                       &desc->channel[desc->swizzle[j]];
+                               if (chan->pure_integer) {
+                                       bcolor32[desc->swizzle[j] + 4] = sampler->border_color.i[j];
+                                       bcolor[desc->swizzle[j] + 8] = sampler->border_color.i[j];
+                               } else {
+                                       bcolor32[desc->swizzle[j]] = fui(sampler->border_color.f[j]);
+                                       bcolor[desc->swizzle[j]] =
+                                               util_float_to_half(sampler->border_color.f[j]);
+                               }
+                       }
+               }
+       }
 }