r300: Debug messages are written to stderr, so fflush that
[mesa.git] / src / mesa / drivers / dri / r300 / compiler / radeon_program_pair.c
index 254431731b32d4d8b9e0a0e286f45020ae9228ab..4c26db5d2441abe3fce8077244545055861bdfe6 100644 (file)
 #include "radeon_program_pair.h"
 
 #include "memory_pool.h"
+#include "radeon_compiler.h"
 #include "shader/prog_print.h"
 
 #define error(fmt, args...) do { \
-       fprintf(stderr, "r300 driver problem: %s::%s(): " fmt "\n",     \
+       rc_error(&s->Compiler->Base, "%s::%s(): " fmt "\n",     \
                __FILE__, __FUNCTION__, ##args);        \
-       s->Error = GL_TRUE;                             \
 } while(0)
 
 struct pair_state_instruction {
@@ -118,11 +118,8 @@ struct pair_register_translation {
 };
 
 struct pair_state {
-       struct memory_pool Pool;
-       struct gl_program *Program;
+       struct r300_fragment_program_compiler * Compiler;
        const struct radeon_pair_handler *Handler;
-       GLboolean Error;
-       GLboolean Debug;
        GLboolean Verbose;
        void *UserData;
 
@@ -335,16 +332,16 @@ static void classify_instruction(struct pair_state *s,
  */
 static void scan_instructions(struct pair_state *s)
 {
-       struct prog_instruction *source;
+       struct rc_instruction *source;
        GLuint ip;
 
-       for(source = s->Program->Instructions, ip = 0;
-           source->Opcode != OPCODE_END;
-           ++source, ++ip) {
-               struct pair_state_instruction *pairinst = memory_pool_malloc(&s->Pool, sizeof(*pairinst));
+       for(source = s->Compiler->Base.Program.Instructions.Next, ip = 0;
+           source != &s->Compiler->Base.Program.Instructions;
+           source = source->Next, ++ip) {
+               struct pair_state_instruction *pairinst = memory_pool_malloc(&s->Compiler->Base.Pool, sizeof(*pairinst));
                memset(pairinst, 0, sizeof(struct pair_state_instruction));
 
-               pairinst->Instruction = *source;
+               pairinst->Instruction = source->I;
                pairinst->IP = ip;
                final_rewrite(s, &pairinst->Instruction);
                classify_instruction(s, pairinst);
@@ -377,7 +374,7 @@ static void scan_instructions(struct pair_state *s)
                                            GET_BIT(pairinst->Instruction.DstReg.WriteMask, swz))
                                                continue;
 
-                                       struct reg_value_reader* r = memory_pool_malloc(&s->Pool, sizeof(*r));
+                                       struct reg_value_reader* r = memory_pool_malloc(&s->Compiler->Base.Pool, sizeof(*r));
                                        pairinst->NumDependencies++;
                                        t->Value[swz]->NumReaders++;
                                        r->Reader = pairinst;
@@ -400,7 +397,7 @@ static void scan_instructions(struct pair_state *s)
                                                if (!GET_BIT(pairinst->Instruction.DstReg.WriteMask, j))
                                                        continue;
 
-                                               struct reg_value* v = memory_pool_malloc(&s->Pool, sizeof(*v));
+                                               struct reg_value* v = memory_pool_malloc(&s->Compiler->Base.Pool, sizeof(*v));
                                                memset(v, 0, sizeof(struct reg_value));
                                                v->Writer = pairinst;
                                                if (t->Value[j]) {
@@ -430,51 +427,6 @@ static void scan_instructions(struct pair_state *s)
 }
 
 
-/**
- * Reserve hardware temporary registers for the program inputs.
- *
- * @note This allocation is performed explicitly, because the order of inputs
- * is determined by the RS hardware.
- */
-static void allocate_input_registers(struct pair_state *s)
-{
-       GLuint InputsRead = s->Program->InputsRead;
-       int i;
-       GLuint hwindex = 0;
-
-       /* Primary colour */
-       if (InputsRead & FRAG_BIT_COL0)
-               alloc_hw_reg(s, PROGRAM_INPUT, FRAG_ATTRIB_COL0, hwindex++);
-       InputsRead &= ~FRAG_BIT_COL0;
-
-       /* Secondary color */
-       if (InputsRead & FRAG_BIT_COL1)
-               alloc_hw_reg(s, PROGRAM_INPUT, FRAG_ATTRIB_COL1, hwindex++);
-       InputsRead &= ~FRAG_BIT_COL1;
-
-       /* Texcoords */
-       for (i = 0; i < 8; i++) {
-               if (InputsRead & (FRAG_BIT_TEX0 << i))
-                       alloc_hw_reg(s, PROGRAM_INPUT, FRAG_ATTRIB_TEX0+i, hwindex++);
-       }
-       InputsRead &= ~FRAG_BITS_TEX_ANY;
-
-       /* Fogcoords treated as a texcoord */
-       if (InputsRead & FRAG_BIT_FOGC)
-               alloc_hw_reg(s, PROGRAM_INPUT, FRAG_ATTRIB_FOGC, hwindex++);
-       InputsRead &= ~FRAG_BIT_FOGC;
-
-       /* fragment position treated as a texcoord */
-       if (InputsRead & FRAG_BIT_WPOS)
-               alloc_hw_reg(s, PROGRAM_INPUT, FRAG_ATTRIB_WPOS, hwindex++);
-       InputsRead &= ~FRAG_BIT_WPOS;
-
-       /* Anything else */
-       if (InputsRead)
-               error("Don't know how to handle inputs 0x%x\n", InputsRead);
-}
-
-
 static void decrement_dependencies(struct pair_state *s, struct pair_state_instruction *pairinst)
 {
        ASSERT(pairinst->NumDependencies > 0);
@@ -580,11 +532,11 @@ static void emit_all_tex(struct pair_state *s)
                        get_hw_reg(s, inst->DstReg.File, inst->DstReg.Index);
        }
 
-       if (s->Debug)
+       if (s->Compiler->Base.Debug)
                _mesa_printf(" BEGIN_TEX\n");
 
        if (s->Handler->BeginTexBlock)
-               s->Error = s->Error || !s->Handler->BeginTexBlock(s->UserData);
+               s->Compiler->Base.Error = s->Compiler->Base.Error || !s->Handler->BeginTexBlock(s->UserData);
 
        for(pairinst = readytex; pairinst; pairinst = pairinst->NextReady) {
                struct prog_instruction *inst = &pairinst->Instruction;
@@ -594,10 +546,10 @@ static void emit_all_tex(struct pair_state *s)
                        inst->DstReg.Index = get_hw_reg(s, inst->DstReg.File, inst->DstReg.Index);
                inst->SrcReg[0].Index = get_hw_reg(s, inst->SrcReg[0].File, inst->SrcReg[0].Index);
 
-               if (s->Debug) {
+               if (s->Compiler->Base.Debug) {
                        _mesa_printf("   ");
                        _mesa_print_instruction(inst);
-                       fflush(stdout);
+                       fflush(stderr);
                }
 
                struct radeon_pair_texture_instruction rpti;
@@ -617,10 +569,10 @@ static void emit_all_tex(struct pair_state *s)
                rpti.SrcIndex = inst->SrcReg[0].Index;
                rpti.SrcSwizzle = inst->SrcReg[0].Swizzle;
 
-               s->Error = s->Error || !s->Handler->EmitTex(s->UserData, &rpti);
+               s->Compiler->Base.Error = s->Compiler->Base.Error || !s->Handler->EmitTex(s->UserData, &rpti);
        }
 
-       if (s->Debug)
+       if (s->Compiler->Base.Debug)
                _mesa_printf(" END_TEX\n");
 }
 
@@ -643,7 +595,7 @@ static int alloc_pair_source(struct pair_state *s, struct radeon_pair_instructio
                index = get_hw_reg(s, src.File, src.Index);
        } else {
                constant = 1;
-               s->Error |= !s->Handler->EmitConst(s->UserData, src.File, src.Index, &index);
+               index = src.Index;
        }
 
        for(i = 0; i < 3; ++i) {
@@ -784,10 +736,10 @@ static void fill_dest_into_pair(
        struct prog_instruction *inst = &pairinst->Instruction;
 
        if (inst->DstReg.File == PROGRAM_OUTPUT) {
-               if (inst->DstReg.Index == FRAG_RESULT_COLOR) {
+               if (inst->DstReg.Index == s->Compiler->OutputColor) {
                        pair->RGB.OutputWriteMask |= inst->DstReg.WriteMask & WRITEMASK_XYZ;
                        pair->Alpha.OutputWriteMask |= GET_BIT(inst->DstReg.WriteMask, 3);
-               } else if (inst->DstReg.Index == FRAG_RESULT_DEPTH) {
+               } else if (inst->DstReg.Index == s->Compiler->OutputDepth) {
                        pair->Alpha.DepthWriteMask |= GET_BIT(inst->DstReg.WriteMask, 3);
                }
        } else {
@@ -867,34 +819,38 @@ static void emit_alu(struct pair_state *s)
        success: ;
        }
 
-       if (s->Debug)
+       if (s->Compiler->Base.Debug)
                radeonPrintPairInstruction(&pair);
 
-       s->Error = s->Error || !s->Handler->EmitPaired(s->UserData, &pair);
+       s->Compiler->Base.Error = s->Compiler->Base.Error || !s->Handler->EmitPaired(s->UserData, &pair);
 }
 
+/* Callback function for assigning input registers to hardware registers */
+static void alloc_helper(void * data, unsigned input, unsigned hwreg)
+{
+       struct pair_state * s = data;
+       alloc_hw_reg(s, PROGRAM_INPUT, input, hwreg);
+}
 
-GLboolean radeonPairProgram(struct gl_program *program,
-       const struct radeon_pair_handler* handler, void *userdata,
-       GLboolean debug)
+void radeonPairProgram(
+       struct r300_fragment_program_compiler * compiler,
+       const struct radeon_pair_handler* handler, void *userdata)
 {
        struct pair_state s;
 
        _mesa_bzero(&s, sizeof(s));
-       memory_pool_init(&s.Pool);
-       s.Program = program;
+       s.Compiler = compiler;
        s.Handler = handler;
        s.UserData = userdata;
-       s.Debug = debug;
-       s.Verbose = GL_FALSE && s.Debug;
+       s.Verbose = GL_FALSE && s.Compiler->Base.Debug;
 
-       if (s.Debug)
+       if (s.Compiler->Base.Debug)
                _mesa_printf("Emit paired program\n");
 
        scan_instructions(&s);
-       allocate_input_registers(&s);
+       s.Compiler->AllocateHwInputs(s.Compiler, &alloc_helper, &s);
 
-       while(!s.Error &&
+       while(!s.Compiler->Base.Error &&
              (s.ReadyTEX || s.ReadyRGB || s.ReadyAlpha || s.ReadyFullALU)) {
                if (s.ReadyTEX)
                        emit_all_tex(&s);
@@ -903,12 +859,8 @@ GLboolean radeonPairProgram(struct gl_program *program,
                        emit_alu(&s);
        }
 
-       if (s.Debug)
+       if (s.Compiler->Base.Debug)
                _mesa_printf(" END\n");
-
-       memory_pool_destroy(&s.Pool);
-
-       return !s.Error;
 }