2006-12-14 15:14:14 -07:00
|
|
|
/*
|
|
|
|
* Mesa 3-D graphics library
|
2007-11-19 13:05:00 -07:00
|
|
|
* Version: 7.0.3
|
2006-12-14 15:14:14 -07:00
|
|
|
*
|
2007-11-19 13:05:00 -07:00
|
|
|
* Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
|
2006-12-14 15:14:14 -07:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
* in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
|
|
|
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2007-07-04 13:15:20 -06:00
|
|
|
#include "main/glheader.h"
|
|
|
|
#include "main/colormac.h"
|
|
|
|
#include "main/context.h"
|
|
|
|
#include "shader/prog_instruction.h"
|
2006-12-14 15:14:14 -07:00
|
|
|
|
2006-12-15 08:50:02 -07:00
|
|
|
#include "s_fragprog.h"
|
2006-12-14 15:14:14 -07:00
|
|
|
#include "s_span.h"
|
|
|
|
|
|
|
|
|
2009-01-28 10:31:05 -07:00
|
|
|
/**
|
|
|
|
* Apply texture object's swizzle (X/Y/Z/W/0/1) to incoming 'texel'
|
|
|
|
* and return results in 'colorOut'.
|
|
|
|
*/
|
|
|
|
static INLINE void
|
2009-03-08 13:49:57 -06:00
|
|
|
swizzle_texel(const GLfloat texel[4], GLfloat colorOut[4], GLuint swizzle)
|
2009-01-28 10:31:05 -07:00
|
|
|
{
|
|
|
|
if (swizzle == SWIZZLE_NOOP) {
|
2009-03-08 13:49:57 -06:00
|
|
|
COPY_4V(colorOut, texel);
|
2009-01-28 10:31:05 -07:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
GLfloat vector[6];
|
2009-03-08 13:49:57 -06:00
|
|
|
vector[SWIZZLE_X] = texel[0];
|
|
|
|
vector[SWIZZLE_Y] = texel[1];
|
|
|
|
vector[SWIZZLE_Z] = texel[2];
|
|
|
|
vector[SWIZZLE_W] = texel[3];
|
2009-01-28 10:31:05 -07:00
|
|
|
vector[SWIZZLE_ZERO] = 0.0F;
|
|
|
|
vector[SWIZZLE_ONE] = 1.0F;
|
|
|
|
colorOut[0] = vector[GET_SWZ(swizzle, 0)];
|
|
|
|
colorOut[1] = vector[GET_SWZ(swizzle, 1)];
|
|
|
|
colorOut[2] = vector[GET_SWZ(swizzle, 2)];
|
|
|
|
colorOut[3] = vector[GET_SWZ(swizzle, 3)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-14 15:14:14 -07:00
|
|
|
/**
|
2008-05-14 16:09:46 -06:00
|
|
|
* Fetch a texel with given lod.
|
|
|
|
* Called via machine->FetchTexelLod()
|
2006-12-14 15:14:14 -07:00
|
|
|
*/
|
|
|
|
static void
|
2008-05-14 16:09:46 -06:00
|
|
|
fetch_texel_lod( GLcontext *ctx, const GLfloat texcoord[4], GLfloat lambda,
|
|
|
|
GLuint unit, GLfloat color[4] )
|
2006-12-14 15:14:14 -07:00
|
|
|
{
|
2007-11-19 13:05:00 -07:00
|
|
|
const struct gl_texture_object *texObj = ctx->Texture.Unit[unit]._Current;
|
2006-12-14 15:14:14 -07:00
|
|
|
|
2009-01-28 09:16:11 -07:00
|
|
|
if (texObj) {
|
|
|
|
SWcontext *swrast = SWRAST_CONTEXT(ctx);
|
2009-03-08 13:49:57 -06:00
|
|
|
GLfloat rgba[4];
|
2009-01-28 09:16:11 -07:00
|
|
|
|
2008-07-08 15:11:23 -06:00
|
|
|
lambda = CLAMP(lambda, texObj->MinLod, texObj->MaxLod);
|
2008-05-14 16:09:46 -06:00
|
|
|
|
2009-01-28 09:16:11 -07:00
|
|
|
swrast->TextureSample[unit](ctx, texObj, 1,
|
|
|
|
(const GLfloat (*)[4]) texcoord,
|
|
|
|
&lambda, &rgba);
|
2009-01-28 10:31:05 -07:00
|
|
|
swizzle_texel(rgba, color, texObj->_Swizzle);
|
2009-01-28 09:16:11 -07:00
|
|
|
}
|
|
|
|
else {
|
2009-02-09 12:43:09 -07:00
|
|
|
ASSIGN_4V(color, 0.0F, 0.0F, 0.0F, 1.0F);
|
2009-01-28 09:16:11 -07:00
|
|
|
}
|
2006-12-14 15:14:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch a texel with the given partial derivatives to compute a level
|
|
|
|
* of detail in the mipmap.
|
2008-05-14 16:09:46 -06:00
|
|
|
* Called via machine->FetchTexelDeriv()
|
2009-09-23 13:35:03 -06:00
|
|
|
* \param lodBias the lod bias which may be specified by a TXB instruction,
|
|
|
|
* otherwise zero.
|
2006-12-14 15:14:14 -07:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
fetch_texel_deriv( GLcontext *ctx, const GLfloat texcoord[4],
|
|
|
|
const GLfloat texdx[4], const GLfloat texdy[4],
|
2007-11-23 12:01:57 -07:00
|
|
|
GLfloat lodBias, GLuint unit, GLfloat color[4] )
|
2006-12-14 15:14:14 -07:00
|
|
|
{
|
|
|
|
SWcontext *swrast = SWRAST_CONTEXT(ctx);
|
2009-09-23 13:35:03 -06:00
|
|
|
const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
|
|
|
|
const struct gl_texture_object *texObj = texUnit->_Current;
|
2006-12-14 15:14:14 -07:00
|
|
|
|
2008-07-08 15:11:23 -06:00
|
|
|
if (texObj) {
|
2009-01-28 09:16:11 -07:00
|
|
|
const struct gl_texture_image *texImg =
|
|
|
|
texObj->Image[0][texObj->BaseLevel];
|
2008-07-08 15:11:23 -06:00
|
|
|
const GLfloat texW = (GLfloat) texImg->WidthScale;
|
|
|
|
const GLfloat texH = (GLfloat) texImg->HeightScale;
|
2009-01-28 09:16:11 -07:00
|
|
|
GLfloat lambda;
|
2009-03-08 13:49:57 -06:00
|
|
|
GLfloat rgba[4];
|
2006-12-14 15:14:14 -07:00
|
|
|
|
2008-07-08 15:11:23 -06:00
|
|
|
lambda = _swrast_compute_lambda(texdx[0], texdy[0], /* ds/dx, ds/dy */
|
|
|
|
texdx[1], texdy[1], /* dt/dx, dt/dy */
|
2009-09-23 12:54:14 -06:00
|
|
|
texdx[3], texdy[3], /* dq/dx, dq/dy */
|
2008-07-08 15:11:23 -06:00
|
|
|
texW, texH,
|
|
|
|
texcoord[0], texcoord[1], texcoord[3],
|
2009-09-23 13:35:03 -06:00
|
|
|
1.0F / texcoord[3]);
|
|
|
|
|
|
|
|
lambda += lodBias + texUnit->LodBias + texObj->LodBias;
|
2008-07-08 15:11:23 -06:00
|
|
|
|
|
|
|
lambda = CLAMP(lambda, texObj->MinLod, texObj->MaxLod);
|
2007-11-23 12:01:57 -07:00
|
|
|
|
2009-01-28 09:16:11 -07:00
|
|
|
swrast->TextureSample[unit](ctx, texObj, 1,
|
|
|
|
(const GLfloat (*)[4]) texcoord,
|
|
|
|
&lambda, &rgba);
|
2009-01-28 10:31:05 -07:00
|
|
|
swizzle_texel(rgba, color, texObj->_Swizzle);
|
2009-01-28 09:16:11 -07:00
|
|
|
}
|
|
|
|
else {
|
2009-02-09 12:43:09 -07:00
|
|
|
ASSIGN_4V(color, 0.0F, 0.0F, 0.0F, 1.0F);
|
2009-01-28 09:16:11 -07:00
|
|
|
}
|
2006-12-14 15:14:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the virtual fragment program machine state prior to running
|
|
|
|
* fragment program on a fragment. This involves initializing the input
|
|
|
|
* registers, condition codes, etc.
|
|
|
|
* \param machine the virtual machine state to init
|
|
|
|
* \param program the fragment program we're about to run
|
|
|
|
* \param span the span of pixels we'll operate on
|
|
|
|
* \param col which element (column) of the span we'll operate on
|
|
|
|
*/
|
|
|
|
static void
|
2007-02-22 16:08:01 -07:00
|
|
|
init_machine(GLcontext *ctx, struct gl_program_machine *machine,
|
|
|
|
const struct gl_fragment_program *program,
|
|
|
|
const SWspan *span, GLuint col)
|
2006-12-14 15:14:14 -07:00
|
|
|
{
|
2010-01-26 12:43:43 -08:00
|
|
|
GLfloat *wpos = span->array->attribs[FRAG_ATTRIB_WPOS][col];
|
|
|
|
|
2007-04-20 08:21:49 -06:00
|
|
|
if (program->Base.Target == GL_FRAGMENT_PROGRAM_NV) {
|
2006-12-14 15:14:14 -07:00
|
|
|
/* Clear temporary registers (undefined for ARB_f_p) */
|
2010-02-19 08:32:36 -07:00
|
|
|
memset(machine->Temporaries, 0, MAX_PROGRAM_TEMPS * 4 * sizeof(GLfloat));
|
2006-12-14 15:14:14 -07:00
|
|
|
}
|
|
|
|
|
2010-01-26 12:43:43 -08:00
|
|
|
/* ARB_fragment_coord_conventions */
|
|
|
|
if (program->OriginUpperLeft)
|
|
|
|
wpos[1] = ctx->DrawBuffer->Height - 1 - wpos[1];
|
|
|
|
if (!program->PixelCenterInteger) {
|
2010-01-27 17:00:32 -07:00
|
|
|
wpos[0] += 0.5F;
|
|
|
|
wpos[1] += 0.5F;
|
2010-01-26 12:43:43 -08:00
|
|
|
}
|
|
|
|
|
2007-02-01 09:51:48 -07:00
|
|
|
/* Setup pointer to input attributes */
|
|
|
|
machine->Attribs = span->array->attribs;
|
2007-03-10 11:30:19 -07:00
|
|
|
|
2007-05-02 18:45:44 -06:00
|
|
|
machine->DerivX = (GLfloat (*)[4]) span->attrStepX;
|
|
|
|
machine->DerivY = (GLfloat (*)[4]) span->attrStepY;
|
|
|
|
machine->NumDeriv = FRAG_ATTRIB_MAX;
|
|
|
|
|
2008-05-14 16:09:46 -06:00
|
|
|
machine->Samplers = program->Base.SamplerUnits;
|
|
|
|
|
2007-10-24 11:37:05 -06:00
|
|
|
/* if running a GLSL program (not ARB_fragment_program) */
|
2007-04-28 08:51:23 -06:00
|
|
|
if (ctx->Shader.CurrentProgram) {
|
2009-07-29 20:07:41 -06:00
|
|
|
/* Store front/back facing value */
|
2010-01-27 17:00:32 -07:00
|
|
|
machine->Attribs[FRAG_ATTRIB_FACE][col][0] = 1.0F - span->facing;
|
2007-04-28 08:51:23 -06:00
|
|
|
}
|
2007-03-10 11:30:19 -07:00
|
|
|
|
2007-02-22 16:08:01 -07:00
|
|
|
machine->CurElement = col;
|
2006-12-14 15:14:14 -07:00
|
|
|
|
|
|
|
/* init condition codes */
|
|
|
|
machine->CondCodes[0] = COND_EQ;
|
|
|
|
machine->CondCodes[1] = COND_EQ;
|
|
|
|
machine->CondCodes[2] = COND_EQ;
|
|
|
|
machine->CondCodes[3] = COND_EQ;
|
|
|
|
|
|
|
|
/* init call stack */
|
|
|
|
machine->StackDepth = 0;
|
2007-02-22 16:08:01 -07:00
|
|
|
|
2008-05-14 16:09:46 -06:00
|
|
|
machine->FetchTexelLod = fetch_texel_lod;
|
2007-02-22 16:08:01 -07:00
|
|
|
machine->FetchTexelDeriv = fetch_texel_deriv;
|
2006-12-14 15:14:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run fragment program on the pixels in span from 'start' to 'end' - 1.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
run_program(GLcontext *ctx, SWspan *span, GLuint start, GLuint end)
|
|
|
|
{
|
2007-03-11 17:00:39 -06:00
|
|
|
SWcontext *swrast = SWRAST_CONTEXT(ctx);
|
2006-12-14 15:14:14 -07:00
|
|
|
const struct gl_fragment_program *program = ctx->FragmentProgram._Current;
|
2009-11-17 16:10:24 -08:00
|
|
|
const GLbitfield64 outputsWritten = program->Base.OutputsWritten;
|
2007-04-19 14:24:10 -06:00
|
|
|
struct gl_program_machine *machine = &swrast->FragProgMachine;
|
2006-12-14 15:14:14 -07:00
|
|
|
GLuint i;
|
|
|
|
|
|
|
|
for (i = start; i < end; i++) {
|
|
|
|
if (span->array->mask[i]) {
|
2007-04-19 14:24:10 -06:00
|
|
|
init_machine(ctx, machine, program, span, i);
|
2006-12-14 15:14:14 -07:00
|
|
|
|
2007-04-19 14:24:10 -06:00
|
|
|
if (_mesa_execute_program(ctx, &program->Base, machine)) {
|
2007-03-11 17:00:39 -06:00
|
|
|
|
2006-12-14 15:14:14 -07:00
|
|
|
/* Store result color */
|
2009-11-17 16:10:24 -08:00
|
|
|
if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_COLOR)) {
|
2007-03-11 17:00:39 -06:00
|
|
|
COPY_4V(span->array->attribs[FRAG_ATTRIB_COL0][i],
|
2009-02-28 11:49:46 -07:00
|
|
|
machine->Outputs[FRAG_RESULT_COLOR]);
|
2007-03-11 17:00:39 -06:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Multiple drawbuffers / render targets
|
|
|
|
* Note that colors beyond 0 and 1 will overwrite other
|
|
|
|
* attributes, such as FOGC, TEX0, TEX1, etc. That's OK.
|
|
|
|
*/
|
2008-01-06 10:43:20 -07:00
|
|
|
GLuint buf;
|
|
|
|
for (buf = 0; buf < ctx->DrawBuffer->_NumColorDrawBuffers; buf++) {
|
2009-11-17 16:10:24 -08:00
|
|
|
if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_DATA0 + buf)) {
|
2008-01-06 10:43:20 -07:00
|
|
|
COPY_4V(span->array->attribs[FRAG_ATTRIB_COL0 + buf][i],
|
|
|
|
machine->Outputs[FRAG_RESULT_DATA0 + buf]);
|
2007-03-11 17:00:39 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-12-14 15:14:14 -07:00
|
|
|
|
|
|
|
/* Store result depth/z */
|
2009-11-17 16:10:24 -08:00
|
|
|
if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
|
2009-02-28 11:49:46 -07:00
|
|
|
const GLfloat depth = machine->Outputs[FRAG_RESULT_DEPTH][2];
|
2006-12-14 15:14:14 -07:00
|
|
|
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 {
|
|
|
|
/* killed fragment */
|
|
|
|
span->array->mask[i] = GL_FALSE;
|
|
|
|
span->writeAll = GL_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the current fragment program for all the fragments
|
|
|
|
* in the given span.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
_swrast_exec_fragment_program( GLcontext *ctx, SWspan *span )
|
|
|
|
{
|
|
|
|
const struct gl_fragment_program *program = ctx->FragmentProgram._Current;
|
|
|
|
|
|
|
|
/* incoming colors should be floats */
|
2007-03-13 10:53:16 -06:00
|
|
|
if (program->Base.InputsRead & FRAG_BIT_COL0) {
|
|
|
|
ASSERT(span->array->ChanType == GL_FLOAT);
|
|
|
|
}
|
2006-12-14 15:14:14 -07:00
|
|
|
|
|
|
|
run_program(ctx, span, 0, span->end);
|
|
|
|
|
2009-11-17 16:10:24 -08:00
|
|
|
if (program->Base.OutputsWritten & BITFIELD64_BIT(FRAG_RESULT_COLOR)) {
|
2007-03-13 10:53:16 -06:00
|
|
|
span->interpMask &= ~SPAN_RGBA;
|
|
|
|
span->arrayMask |= SPAN_RGBA;
|
|
|
|
}
|
|
|
|
|
2009-11-17 16:10:24 -08:00
|
|
|
if (program->Base.OutputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
|
2006-12-14 15:14:14 -07:00
|
|
|
span->interpMask &= ~SPAN_Z;
|
|
|
|
span->arrayMask |= SPAN_Z;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|