i965/gs: implement EndPrimitive() functionality in the visitor.
[mesa.git] / src / mesa / drivers / dri / nouveau / nv10_state_frag.c
index c1df26ecce6cf5b35dc4a72acfc9e9e810500a2e..467b762fa0330ee50d751803f12b189ce43d2b45 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009 Francisco Jerez.
+ * Copyright (C) 2009-2010 Francisco Jerez.
  * All Rights Reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining
@@ -27,7 +27,7 @@
 #include "nouveau_driver.h"
 #include "nouveau_context.h"
 #include "nouveau_gldefs.h"
-#include "nouveau_class.h"
+#include "nv10_3d.xml.h"
 #include "nouveau_util.h"
 #include "nv10_driver.h"
 #include "nv20_driver.h"
 #define RC_IN_SHIFT_G  40
 
 #define RC_IN_SOURCE(source)                           \
-       ((uint64_t)NV10TCL_RC_IN_RGB_D_INPUT_##source)
+       ((uint64_t)NV10_3D_RC_IN_RGB_D_INPUT_##source)
 #define RC_IN_USAGE(usage)                                     \
-       ((uint64_t)NV10TCL_RC_IN_RGB_D_COMPONENT_USAGE_##usage)
+       ((uint64_t)NV10_3D_RC_IN_RGB_D_COMPONENT_USAGE_##usage)
 #define RC_IN_MAPPING(mapping)                                 \
-       ((uint64_t)NV10TCL_RC_IN_RGB_D_MAPPING_##mapping)
+       ((uint64_t)NV10_3D_RC_IN_RGB_D_MAPPING_##mapping)
 
-#define RC_OUT_BIAS    NV10TCL_RC_OUT_RGB_BIAS_BIAS_BY_NEGATIVE_ONE_HALF
-#define RC_OUT_SCALE_1 NV10TCL_RC_OUT_RGB_SCALE_NONE
-#define RC_OUT_SCALE_2 NV10TCL_RC_OUT_RGB_SCALE_SCALE_BY_TWO
-#define RC_OUT_SCALE_4 NV10TCL_RC_OUT_RGB_SCALE_SCALE_BY_FOUR
+#define RC_OUT_BIAS    NV10_3D_RC_OUT_RGB_BIAS_BIAS_BY_NEGATIVE_ONE_HALF
+#define RC_OUT_SCALE_1 NV10_3D_RC_OUT_RGB_SCALE_NONE
+#define RC_OUT_SCALE_2 NV10_3D_RC_OUT_RGB_SCALE_SCALE_BY_TWO
+#define RC_OUT_SCALE_4 NV10_3D_RC_OUT_RGB_SCALE_SCALE_BY_FOUR
 
 /* Make the combiner do: spare0_i = A_i * B_i */
-#define RC_OUT_AB      NV10TCL_RC_OUT_RGB_AB_OUTPUT_SPARE0
+#define RC_OUT_AB      NV10_3D_RC_OUT_RGB_AB_OUTPUT_SPARE0
 /* spare0_i = dot3(A, B) */
-#define RC_OUT_DOT_AB  (NV10TCL_RC_OUT_RGB_AB_OUTPUT_SPARE0 |  \
-                        NV10TCL_RC_OUT_RGB_AB_DOT_PRODUCT)
+#define RC_OUT_DOT_AB  (NV10_3D_RC_OUT_RGB_AB_OUTPUT_SPARE0 |  \
+                        NV10_3D_RC_OUT_RGB_AB_DOT_PRODUCT)
 /* spare0_i = A_i * B_i + C_i * D_i */
-#define RC_OUT_SUM     NV10TCL_RC_OUT_RGB_SUM_OUTPUT_SPARE0
+#define RC_OUT_SUM     NV10_3D_RC_OUT_RGB_SUM_OUTPUT_SPARE0
 
 struct combiner_state {
-       GLcontext *ctx;
+       struct gl_context *ctx;
        int unit;
+       GLboolean premodulate;
 
        /* GL state */
        GLenum mode;
@@ -82,6 +83,7 @@ struct combiner_state {
                        ctx->Texture.Unit[i]._CurrentCombine;   \
                (rc)->ctx = ctx;                                \
                (rc)->unit = i;                                 \
+               (rc)->premodulate = c->_NumArgs##chan == 4;     \
                (rc)->mode = c->Mode##chan;                     \
                (rc)->source = c->Source##chan;                 \
                (rc)->operand = c->Operand##chan;               \
@@ -90,11 +92,14 @@ struct combiner_state {
        } while (0)
 
 /* Get the RC input source for the specified EXT_texture_env_combine
- * argument. */
+ * source. */
 static uint32_t
-get_input_source(struct combiner_state *rc, int arg)
+get_input_source(struct combiner_state *rc, int source)
 {
-       switch (rc->source[arg]) {
+       switch (source) {
+       case GL_ZERO:
+               return RC_IN_SOURCE(ZERO);
+
        case GL_TEXTURE:
                return RC_IN_SOURCE(TEXTURE0) + rc->unit;
 
@@ -127,52 +132,76 @@ get_input_source(struct combiner_state *rc, int arg)
        }
 }
 
-/* Get the RC input mapping for the specified argument, possibly
- * inverted or biased. */
+/* Get the RC input mapping for the specified texture_env_combine
+ * operand, possibly inverted or biased. */
 #define INVERT 0x1
 #define HALF_BIAS 0x2
 
 static uint32_t
-get_input_mapping(struct combiner_state *rc, int arg, int flags)
+get_input_mapping(struct combiner_state *rc, int operand, int flags)
 {
        int map = 0;
 
-       switch (rc->operand[arg]) {
-       case GL_SRC_COLOR:
-       case GL_ONE_MINUS_SRC_COLOR:
+       if (is_color_operand(operand))
                map |= RC_IN_USAGE(RGB);
-               break;
-
-       case GL_SRC_ALPHA:
-       case GL_ONE_MINUS_SRC_ALPHA:
+       else
                map |= RC_IN_USAGE(ALPHA);
-               break;
-       }
 
-       switch (rc->operand[arg]) {
-       case GL_SRC_COLOR:
-       case GL_SRC_ALPHA:
-               map |= (flags & INVERT ? RC_IN_MAPPING(UNSIGNED_INVERT) :
-                       flags & HALF_BIAS ? RC_IN_MAPPING(HALF_BIAS_NORMAL) :
-                       RC_IN_MAPPING(UNSIGNED_IDENTITY));
-               break;
+       if (is_negative_operand(operand) == !(flags & INVERT))
+               map |= flags & HALF_BIAS ?
+                       RC_IN_MAPPING(HALF_BIAS_NEGATE) :
+                       RC_IN_MAPPING(UNSIGNED_INVERT);
+       else
+               map |= flags & HALF_BIAS ?
+                       RC_IN_MAPPING(HALF_BIAS_NORMAL) :
+                       RC_IN_MAPPING(UNSIGNED_IDENTITY);
 
-       case GL_ONE_MINUS_SRC_COLOR:
-       case GL_ONE_MINUS_SRC_ALPHA:
-               map |= (flags & INVERT ? RC_IN_MAPPING(UNSIGNED_IDENTITY) :
-                       flags & HALF_BIAS ? RC_IN_MAPPING(HALF_BIAS_NEGATE) :
-                       RC_IN_MAPPING(UNSIGNED_INVERT));
-               break;
+       return map;
+}
+
+static uint32_t
+get_input_arg(struct combiner_state *rc, int arg, int flags)
+{
+       int source = rc->source[arg];
+       int operand = rc->operand[arg];
+
+       /* Fake several unsupported texture formats. */
+       if (is_texture_source(source)) {
+               int i = (source == GL_TEXTURE ?
+                        rc->unit : source - GL_TEXTURE0);
+               struct gl_texture_object *t = rc->ctx->Texture.Unit[i]._Current;
+               gl_format format = t->Image[0][t->BaseLevel]->TexFormat;
+
+               if (format == MESA_FORMAT_A8) {
+                       /* Emulated using I8. */
+                       if (is_color_operand(operand))
+                               return RC_IN_SOURCE(ZERO) |
+                                       get_input_mapping(rc, operand, flags);
+
+               } else if (format == MESA_FORMAT_L8) {
+                       /* Sometimes emulated using I8. */
+                       if (!is_color_operand(operand))
+                               return RC_IN_SOURCE(ZERO) |
+                                       get_input_mapping(rc, operand,
+                                                         flags ^ INVERT);
+
+               } else if (format == MESA_FORMAT_XRGB8888) {
+                       /* Sometimes emulated using ARGB8888. */
+                       if (!is_color_operand(operand))
+                               return RC_IN_SOURCE(ZERO) |
+                                       get_input_mapping(rc, operand,
+                                                         flags ^ INVERT);
+               }
        }
 
-       return map;
+       return get_input_source(rc, source) |
+               get_input_mapping(rc, operand, flags);
 }
 
 /* Bind the RC input variable <var> to the EXT_texture_env_combine
  * argument <arg>, possibly inverted or biased. */
 #define INPUT_ARG(rc, var, arg, flags)                                 \
-       (rc)->in |= (get_input_mapping(rc, arg, flags) |                \
-                    get_input_source(rc, arg)) << RC_IN_SHIFT_##var
+       (rc)->in |= get_input_arg(rc, arg, flags) << RC_IN_SHIFT_##var
 
 /* Bind the RC input variable <var> to the RC source <src>. */
 #define INPUT_SRC(rc, var, src, chan)                                  \
@@ -204,21 +233,21 @@ setup_combiner(struct combiner_state *rc)
                break;
 
        case GL_ADD:
-               INPUT_ARG(rc, A, 0, 0);
-               INPUT_ONE(rc, B, 0);
-               INPUT_ARG(rc, C, 1, 0);
-               INPUT_ONE(rc, D, 0);
-
-               rc->out = RC_OUT_SUM;
-               break;
-
        case GL_ADD_SIGNED:
-               INPUT_ARG(rc, A, 0, 0);
-               INPUT_ONE(rc, B, 0);
-               INPUT_ARG(rc, C, 1, 0);
-               INPUT_ONE(rc, D, 0);
-
-               rc->out = RC_OUT_SUM | RC_OUT_BIAS;
+               if (rc->premodulate) {
+                       INPUT_ARG(rc, A, 0, 0);
+                       INPUT_ARG(rc, B, 1, 0);
+                       INPUT_ARG(rc, C, 2, 0);
+                       INPUT_ARG(rc, D, 3, 0);
+               } else {
+                       INPUT_ARG(rc, A, 0, 0);
+                       INPUT_ONE(rc, B, 0);
+                       INPUT_ARG(rc, C, 1, 0);
+                       INPUT_ONE(rc, D, 0);
+               }
+
+               rc->out = RC_OUT_SUM |
+                       (rc->mode == GL_ADD_SIGNED ? RC_OUT_BIAS : 0);
                break;
 
        case GL_INTERPOLATE:
@@ -268,86 +297,13 @@ setup_combiner(struct combiner_state *rc)
        }
 }
 
-/* Write the register combiner state out to the hardware. */
-static void
-nv10_load_combiner(GLcontext *ctx, int i, struct combiner_state *rc_a,
-                  struct combiner_state *rc_c, uint32_t rc_const)
-{
-       struct nouveau_channel *chan = context_chan(ctx);
-       struct nouveau_grobj *celsius = context_eng3d(ctx);
-
-       /* Enable the combiners we're going to need. */
-       if (i == 1) {
-               if (rc_c->out || rc_a->out)
-                       rc_c->out |= 0x5 << 27;
-               else
-                       rc_c->out |= 0x3 << 27;
-       }
-
-       BEGIN_RING(chan, celsius, NV10TCL_RC_IN_ALPHA(i), 1);
-       OUT_RING(chan, rc_a->in);
-       BEGIN_RING(chan, celsius, NV10TCL_RC_IN_RGB(i), 1);
-       OUT_RING(chan, rc_c->in);
-       BEGIN_RING(chan, celsius, NV10TCL_RC_COLOR(i), 1);
-       OUT_RING(chan, rc_const);
-       BEGIN_RING(chan, celsius, NV10TCL_RC_OUT_ALPHA(i), 1);
-       OUT_RING(chan, rc_a->out);
-       BEGIN_RING(chan, celsius, NV10TCL_RC_OUT_RGB(i), 1);
-       OUT_RING(chan, rc_c->out);
-}
-
-static void
-nv10_load_final(GLcontext *ctx, struct combiner_state *rc, int n)
-{
-       struct nouveau_channel *chan = context_chan(ctx);
-       struct nouveau_grobj *celsius = context_eng3d(ctx);
-
-       BEGIN_RING(chan, celsius, NV10TCL_RC_FINAL0, 2);
-       OUT_RING(chan, rc->in);
-       OUT_RING(chan, rc->in >> 32);
-}
-
-static void
-nv20_load_combiner(GLcontext *ctx, int i, struct combiner_state *rc_a,
-                  struct combiner_state *rc_c, uint32_t rc_const)
-{
-       struct nouveau_channel *chan = context_chan(ctx);
-       struct nouveau_grobj *kelvin = context_eng3d(ctx);
-
-       BEGIN_RING(chan, kelvin, NV20TCL_RC_IN_ALPHA(i), 1);
-       OUT_RING(chan, rc_a->in);
-       BEGIN_RING(chan, kelvin, NV20TCL_RC_OUT_ALPHA(i), 1);
-       OUT_RING(chan, rc_a->out);
-       BEGIN_RING(chan, kelvin, NV20TCL_RC_IN_RGB(i), 1);
-       OUT_RING(chan, rc_c->in);
-       BEGIN_RING(chan, kelvin, NV20TCL_RC_OUT_RGB(i), 1);
-       OUT_RING(chan, rc_c->out);
-       BEGIN_RING(chan, kelvin, NV20TCL_RC_CONSTANT_COLOR0(i), 1);
-       OUT_RING(chan, rc_const);
-}
-
-static void
-nv20_load_final(GLcontext *ctx, struct combiner_state *rc, int n)
-{
-       struct nouveau_channel *chan = context_chan(ctx);
-       struct nouveau_grobj *kelvin = context_eng3d(ctx);
-
-       BEGIN_RING(chan, kelvin, NV20TCL_RC_FINAL0, 2);
-       OUT_RING(chan, rc->in);
-       OUT_RING(chan, rc->in >> 32);
-
-       BEGIN_RING(chan, kelvin, NV20TCL_RC_ENABLE, 1);
-       OUT_RING(chan, n);
-}
-
 void
-nv10_emit_tex_env(GLcontext *ctx, int emit)
+nv10_get_general_combiner(struct gl_context *ctx, int i,
+                         uint32_t *a_in, uint32_t *a_out,
+                         uint32_t *c_in, uint32_t *c_out, uint32_t *k)
 {
-       const int i = emit - NOUVEAU_STATE_TEX_ENV0;
        struct combiner_state rc_a, rc_c;
-       uint32_t rc_const;
 
-       /* Compute the new combiner state. */
        if (ctx->Texture.Unit[i]._ReallyEnabled) {
                INIT_COMBINER(RGB, ctx, &rc_c, i);
 
@@ -359,26 +315,22 @@ nv10_emit_tex_env(GLcontext *ctx, int emit)
                setup_combiner(&rc_c);
                setup_combiner(&rc_a);
 
-               rc_const = pack_rgba_f(MESA_FORMAT_ARGB8888,
-                                      ctx->Texture.Unit[i].EnvColor);
-
        } else {
-               rc_a.in = rc_a.out = rc_c.in = rc_c.out = rc_const = 0;
+               rc_a.in = rc_a.out = rc_c.in = rc_c.out = 0;
        }
 
-       if (context_chipset(ctx) >= 0x20)
-               nv20_load_combiner(ctx, i, &rc_a, &rc_c, rc_const);
-       else
-               nv10_load_combiner(ctx, i, &rc_a, &rc_c, rc_const);
-
-       context_dirty(ctx, FRAG);
+       *k = pack_rgba_f(MESA_FORMAT_ARGB8888,
+                        ctx->Texture.Unit[i].EnvColor);
+       *a_in = rc_a.in;
+       *a_out = rc_a.out;
+       *c_in = rc_c.in;
+       *c_out = rc_c.out;
 }
 
 void
-nv10_emit_frag(GLcontext *ctx, int emit)
+nv10_get_final_combiner(struct gl_context *ctx, uint64_t *in, int *n)
 {
        struct combiner_state rc = {};
-       int n = log2i(ctx->Texture._EnabledUnits) + 1;
 
        /*
         * The final fragment value equation is something like:
@@ -409,8 +361,51 @@ nv10_emit_frag(GLcontext *ctx, int emit)
                INPUT_SRC(&rc, G, PRIMARY_COLOR, ALPHA);
        }
 
-       if (context_chipset(ctx) >= 0x20)
-               nv20_load_final(ctx, &rc, n);
-       else
-               nv10_load_final(ctx, &rc, n);
+       *in = rc.in;
+       *n = log2i(ctx->Texture._EnabledUnits) + 1;
+}
+
+void
+nv10_emit_tex_env(struct gl_context *ctx, int emit)
+{
+       const int i = emit - NOUVEAU_STATE_TEX_ENV0;
+       struct nouveau_pushbuf *push = context_push(ctx);
+       uint32_t a_in, a_out, c_in, c_out, k;
+
+       nv10_get_general_combiner(ctx, i, &a_in, &a_out, &c_in, &c_out, &k);
+
+       /* Enable the combiners we're going to need. */
+       if (i == 1) {
+               if (c_out || a_out)
+                       c_out |= 0x5 << 27;
+               else
+                       c_out |= 0x3 << 27;
+       }
+
+       BEGIN_NV04(push, NV10_3D(RC_IN_ALPHA(i)), 1);
+       PUSH_DATA (push, a_in);
+       BEGIN_NV04(push, NV10_3D(RC_IN_RGB(i)), 1);
+       PUSH_DATA (push, c_in);
+       BEGIN_NV04(push, NV10_3D(RC_COLOR(i)), 1);
+       PUSH_DATA (push, k);
+       BEGIN_NV04(push, NV10_3D(RC_OUT_ALPHA(i)), 1);
+       PUSH_DATA (push, a_out);
+       BEGIN_NV04(push, NV10_3D(RC_OUT_RGB(i)), 1);
+       PUSH_DATA (push, c_out);
+
+       context_dirty(ctx, FRAG);
+}
+
+void
+nv10_emit_frag(struct gl_context *ctx, int emit)
+{
+       struct nouveau_pushbuf *push = context_push(ctx);
+       uint64_t in;
+       int n;
+
+       nv10_get_final_combiner(ctx, &in, &n);
+
+       BEGIN_NV04(push, NV10_3D(RC_FINAL0), 2);
+       PUSH_DATA (push, in);
+       PUSH_DATA (push, in >> 32);
 }