#include "glheader.h"
#include "colormac.h"
#include "context.h"
#include "prog_instruction.h"
#include "s_fragprog.h"
#include "s_span.h"
static void
fetch_texel( GLcontext *ctx, const GLfloat texcoord[4], GLfloat lambda,
GLuint unit, GLfloat color[4] )
{
GLchan rgba[4];
SWcontext *swrast = SWRAST_CONTEXT(ctx);
const struct gl_texture_object *texObj = ctx->Texture.Unit[unit]._Current;
if (texObj)
lambda = CLAMP(lambda, texObj->MinLod, texObj->MaxLod);
swrast->TextureSample[unit](ctx, texObj, 1, (const GLfloat (*)[4]) texcoord,
&lambda, &rgba);
color[0] = CHAN_TO_FLOAT(rgba[0]);
color[1] = CHAN_TO_FLOAT(rgba[1]);
color[2] = CHAN_TO_FLOAT(rgba[2]);
color[3] = CHAN_TO_FLOAT(rgba[3]);
}
static void
fetch_texel_deriv( GLcontext *ctx, const GLfloat texcoord[4],
const GLfloat texdx[4], const GLfloat texdy[4],
GLfloat lodBias, GLuint unit, GLfloat color[4] )
{
SWcontext *swrast = SWRAST_CONTEXT(ctx);
const struct gl_texture_object *texObj = ctx->Texture.Unit[unit]._Current;
GLfloat lambda;
GLchan rgba[4];
if (texObj) {
const struct gl_texture_image *texImg = texObj->Image[0][texObj->BaseLevel];
const GLfloat texW = (GLfloat) texImg->WidthScale;
const GLfloat texH = (GLfloat) texImg->HeightScale;
lambda = _swrast_compute_lambda(texdx[0], texdy[0],
texdx[1], texdy[1],
texdx[3], texdy[2],
texW, texH,
texcoord[0], texcoord[1], texcoord[3],
1.0F / texcoord[3]) + lodBias;
lambda = CLAMP(lambda, texObj->MinLod, texObj->MaxLod);
}
swrast->TextureSample[unit](ctx, texObj, 1, (const GLfloat (*)[4]) texcoord,
&lambda, &rgba);
color[0] = CHAN_TO_FLOAT(rgba[0]);
color[1] = CHAN_TO_FLOAT(rgba[1]);
color[2] = CHAN_TO_FLOAT(rgba[2]);
color[3] = CHAN_TO_FLOAT(rgba[3]);
}
static void
init_machine(GLcontext *ctx, struct gl_program_machine *machine,
const struct gl_fragment_program *program,
const SWspan *span, GLuint col)
{
if (program->Base.Target == GL_FRAGMENT_PROGRAM_NV) {
_mesa_bzero(machine->Temporaries,
MAX_PROGRAM_TEMPS * 4 * sizeof(GLfloat));
}
machine->Attribs = span->array->attribs;
machine->DerivX = (GLfloat (*)[4]) span->attrStepX;
machine->DerivY = (GLfloat (*)[4]) span->attrStepY;
machine->NumDeriv = FRAG_ATTRIB_MAX;
if (ctx->Shader.CurrentProgram) {
machine->Attribs[FRAG_ATTRIB_FOGC][col][1] = 1.0 - span->facing;
}
machine->CurElement = col;
machine->CondCodes[0] = COND_EQ;
machine->CondCodes[1] = COND_EQ;
machine->CondCodes[2] = COND_EQ;
machine->CondCodes[3] = COND_EQ;
machine->StackDepth = 0;
machine->FetchTexelLod = fetch_texel;
machine->FetchTexelDeriv = fetch_texel_deriv;
}
static void
run_program(GLcontext *ctx, SWspan *span, GLuint start, GLuint end)
{
SWcontext *swrast = SWRAST_CONTEXT(ctx);
const struct gl_fragment_program *program = ctx->FragmentProgram._Current;
const GLbitfield outputsWritten = program->Base.OutputsWritten;
struct gl_program_machine *machine = &swrast->FragProgMachine;
GLuint i;
for (i = start; i < end; i++) {
if (span->array->mask[i]) {
init_machine(ctx, machine, program, span, i);
if (_mesa_execute_program(ctx, &program->Base, machine)) {
if (outputsWritten & (1 << FRAG_RESULT_COLR)) {
COPY_4V(span->array->attribs[FRAG_ATTRIB_COL0][i],
machine->Outputs[FRAG_RESULT_COLR]);
}
else {
GLuint output;
for (output = 0; output < swrast->_NumColorOutputs; output++) {
if (outputsWritten & (1 << (FRAG_RESULT_DATA0 + output))) {
COPY_4V(span->array->attribs[FRAG_ATTRIB_COL0+output][i],
machine->Outputs[FRAG_RESULT_DATA0 + output]);
}
}
}
if (outputsWritten & (1 << FRAG_RESULT_DEPR)) {
const GLfloat depth = machine->Outputs[FRAG_RESULT_DEPR][2];
if (depth <= 0.0)
span->array->z[i] = 0;
else if (depth >= 1.0)
span->array->z[i] = ctx->DrawBuffer->_DepthMax;
else
span->array->z[i] = IROUND(depth * ctx->DrawBuffer->_DepthMaxF);
}
}
else {
span->array->mask[i] = GL_FALSE;
span->writeAll = GL_FALSE;
}
}
}
}
void
_swrast_exec_fragment_program( GLcontext *ctx, SWspan *span )
{
const struct gl_fragment_program *program = ctx->FragmentProgram._Current;
if (program->Base.InputsRead & FRAG_BIT_COL0) {
ASSERT(span->array->ChanType == GL_FLOAT);
}
ctx->_CurrentProgram = GL_FRAGMENT_PROGRAM_ARB;
run_program(ctx, span, 0, span->end);
if (program->Base.OutputsWritten & (1 << FRAG_RESULT_COLR)) {
span->interpMask &= ~SPAN_RGBA;
span->arrayMask |= SPAN_RGBA;
}
if (program->Base.OutputsWritten & (1 << FRAG_RESULT_DEPR)) {
span->interpMask &= ~SPAN_Z;
span->arrayMask |= SPAN_Z;
}
ctx->_CurrentProgram = 0;
}