+ * Initialize a new machine state instance from an existing one, adding
+ * the partial derivatives onto the input registers.
+ * Used to implement DDX and DDY instructions in non-trivial cases.
+ */
+static void
+init_machine_deriv( GLcontext *ctx,
+ const struct fp_machine *machine,
+ const struct gl_fragment_program *program,
+ const SWspan *span, char xOrY,
+ struct fp_machine *dMachine )
+{
+ GLuint u;
+
+ ASSERT(xOrY == 'X' || xOrY == 'Y');
+
+ /* copy existing machine */
+ _mesa_memcpy(dMachine, machine, sizeof(struct fp_machine));
+
+ if (program->Base.Target == GL_FRAGMENT_PROGRAM_NV) {
+ /* Clear temporary registers (undefined for ARB_f_p) */
+ _mesa_bzero( (void*) machine->Temporaries,
+ MAX_NV_FRAGMENT_PROGRAM_TEMPS * 4 * sizeof(GLfloat));
+ }
+
+ /* Add derivatives */
+ if (program->Base.InputsRead & (1 << FRAG_ATTRIB_WPOS)) {
+ GLfloat *wpos = (GLfloat*) machine->Inputs[FRAG_ATTRIB_WPOS];
+ if (xOrY == 'X') {
+ wpos[0] += 1.0F;
+ wpos[1] += 0.0F;
+ wpos[2] += span->dzdx;
+ wpos[3] += span->dwdx;
+ }
+ else {
+ wpos[0] += 0.0F;
+ wpos[1] += 1.0F;
+ wpos[2] += span->dzdy;
+ wpos[3] += span->dwdy;
+ }
+ }
+ if (program->Base.InputsRead & (1 << FRAG_ATTRIB_COL0)) {
+ GLfloat *col0 = (GLfloat*) machine->Inputs[FRAG_ATTRIB_COL0];
+ if (xOrY == 'X') {
+ col0[0] += span->drdx * (1.0F / CHAN_MAXF);
+ col0[1] += span->dgdx * (1.0F / CHAN_MAXF);
+ col0[2] += span->dbdx * (1.0F / CHAN_MAXF);
+ col0[3] += span->dadx * (1.0F / CHAN_MAXF);
+ }
+ else {
+ col0[0] += span->drdy * (1.0F / CHAN_MAXF);
+ col0[1] += span->dgdy * (1.0F / CHAN_MAXF);
+ col0[2] += span->dbdy * (1.0F / CHAN_MAXF);
+ col0[3] += span->dady * (1.0F / CHAN_MAXF);
+ }
+ }
+ if (program->Base.InputsRead & (1 << FRAG_ATTRIB_COL1)) {
+ GLfloat *col1 = (GLfloat*) machine->Inputs[FRAG_ATTRIB_COL1];
+ if (xOrY == 'X') {
+ col1[0] += span->dsrdx * (1.0F / CHAN_MAXF);
+ col1[1] += span->dsgdx * (1.0F / CHAN_MAXF);
+ col1[2] += span->dsbdx * (1.0F / CHAN_MAXF);
+ col1[3] += 0.0; /*XXX fix */
+ }
+ else {
+ col1[0] += span->dsrdy * (1.0F / CHAN_MAXF);
+ col1[1] += span->dsgdy * (1.0F / CHAN_MAXF);
+ col1[2] += span->dsbdy * (1.0F / CHAN_MAXF);
+ col1[3] += 0.0; /*XXX fix */
+ }
+ }
+ if (program->Base.InputsRead & (1 << FRAG_ATTRIB_FOGC)) {
+ GLfloat *fogc = (GLfloat*) machine->Inputs[FRAG_ATTRIB_FOGC];
+ if (xOrY == 'X') {
+ fogc[0] += span->dfogdx;
+ }
+ else {
+ fogc[0] += span->dfogdy;
+ }
+ }
+ for (u = 0; u < ctx->Const.MaxTextureCoordUnits; u++) {
+ if (program->Base.InputsRead & (1 << (FRAG_ATTRIB_TEX0 + u))) {
+ GLfloat *tex = (GLfloat*) machine->Inputs[FRAG_ATTRIB_TEX0 + u];
+ /* XXX perspective-correct interpolation */
+ if (xOrY == 'X') {
+ tex[0] += span->texStepX[u][0];
+ tex[1] += span->texStepX[u][1];
+ tex[2] += span->texStepX[u][2];
+ tex[3] += span->texStepX[u][3];
+ }
+ else {
+ tex[0] += span->texStepY[u][0];
+ tex[1] += span->texStepY[u][1];
+ tex[2] += span->texStepY[u][2];
+ tex[3] += span->texStepY[u][3];
+ }
+ }
+ }
+
+ /* init condition codes */
+ dMachine->CondCodes[0] = COND_EQ;
+ dMachine->CondCodes[1] = COND_EQ;
+ dMachine->CondCodes[2] = COND_EQ;
+ dMachine->CondCodes[3] = COND_EQ;
+}
+
+
+/**
+ * Execute the given vertex program.
+ * NOTE: we do everything in single-precision floating point; we don't
+ * currently observe the single/half/fixed-precision qualifiers.
+ * \param ctx - rendering context
+ * \param program - the fragment program to execute
+ * \param machine - machine state (register file)
+ * \param maxInst - max number of instructions to execute