blob: 9facb44d9bf9bca198a626e853c26e7278858496 [file] [log] [blame]
Brian865f88a2006-12-14 15:14:14 -07001/*
2 * Mesa 3-D graphics library
Brian827e72d2007-11-19 13:05:00 -07003 * Version: 7.0.3
Brian865f88a2006-12-14 15:14:14 -07004 *
Brian827e72d2007-11-19 13:05:00 -07005 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
Brian865f88a2006-12-14 15:14:14 -07006 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
Brianc223c6b2007-07-04 13:15:20 -060025#include "main/glheader.h"
26#include "main/colormac.h"
Brian Paulec2b92f2010-06-10 23:02:41 -060027#include "program/prog_instruction.h"
Brian865f88a2006-12-14 15:14:14 -070028
Vinson Leef009f172010-08-07 21:01:12 -070029#include "s_context.h"
Brianc968d3d2006-12-15 08:50:02 -070030#include "s_fragprog.h"
Brian865f88a2006-12-14 15:14:14 -070031#include "s_span.h"
32
33
Brian865f88a2006-12-14 15:14:14 -070034/**
Brian Paul54c62ba2009-01-28 10:31:05 -070035 * Apply texture object's swizzle (X/Y/Z/W/0/1) to incoming 'texel'
36 * and return results in 'colorOut'.
37 */
38static INLINE void
Brian Paulde2afd82009-03-08 13:49:57 -060039swizzle_texel(const GLfloat texel[4], GLfloat colorOut[4], GLuint swizzle)
Brian Paul54c62ba2009-01-28 10:31:05 -070040{
41 if (swizzle == SWIZZLE_NOOP) {
Brian Paulde2afd82009-03-08 13:49:57 -060042 COPY_4V(colorOut, texel);
Brian Paul54c62ba2009-01-28 10:31:05 -070043 }
44 else {
45 GLfloat vector[6];
Brian Paulde2afd82009-03-08 13:49:57 -060046 vector[SWIZZLE_X] = texel[0];
47 vector[SWIZZLE_Y] = texel[1];
48 vector[SWIZZLE_Z] = texel[2];
49 vector[SWIZZLE_W] = texel[3];
Brian Paul54c62ba2009-01-28 10:31:05 -070050 vector[SWIZZLE_ZERO] = 0.0F;
51 vector[SWIZZLE_ONE] = 1.0F;
52 colorOut[0] = vector[GET_SWZ(swizzle, 0)];
53 colorOut[1] = vector[GET_SWZ(swizzle, 1)];
54 colorOut[2] = vector[GET_SWZ(swizzle, 2)];
55 colorOut[3] = vector[GET_SWZ(swizzle, 3)];
56 }
57}
58
59
60/**
Brian Paulade50832008-05-14 16:09:46 -060061 * Fetch a texel with given lod.
62 * Called via machine->FetchTexelLod()
Brian865f88a2006-12-14 15:14:14 -070063 */
64static void
Brian Paulade50832008-05-14 16:09:46 -060065fetch_texel_lod( GLcontext *ctx, const GLfloat texcoord[4], GLfloat lambda,
66 GLuint unit, GLfloat color[4] )
Brian865f88a2006-12-14 15:14:14 -070067{
Brian827e72d2007-11-19 13:05:00 -070068 const struct gl_texture_object *texObj = ctx->Texture.Unit[unit]._Current;
Brian865f88a2006-12-14 15:14:14 -070069
Brian Paulbe1a76f2009-01-28 09:16:11 -070070 if (texObj) {
71 SWcontext *swrast = SWRAST_CONTEXT(ctx);
Brian Paulde2afd82009-03-08 13:49:57 -060072 GLfloat rgba[4];
Brian Paulbe1a76f2009-01-28 09:16:11 -070073
Brian Paulc71fa342008-07-08 15:11:23 -060074 lambda = CLAMP(lambda, texObj->MinLod, texObj->MaxLod);
Brian Paulade50832008-05-14 16:09:46 -060075
Brian Paulbe1a76f2009-01-28 09:16:11 -070076 swrast->TextureSample[unit](ctx, texObj, 1,
77 (const GLfloat (*)[4]) texcoord,
78 &lambda, &rgba);
Brian Paul54c62ba2009-01-28 10:31:05 -070079 swizzle_texel(rgba, color, texObj->_Swizzle);
Brian Paulbe1a76f2009-01-28 09:16:11 -070080 }
81 else {
Brian Paul88f36562009-02-09 12:43:09 -070082 ASSIGN_4V(color, 0.0F, 0.0F, 0.0F, 1.0F);
Brian Paulbe1a76f2009-01-28 09:16:11 -070083 }
Brian865f88a2006-12-14 15:14:14 -070084}
85
86
87/**
88 * Fetch a texel with the given partial derivatives to compute a level
89 * of detail in the mipmap.
Brian Paulade50832008-05-14 16:09:46 -060090 * Called via machine->FetchTexelDeriv()
Brian Paul2acd5de2009-09-23 13:35:03 -060091 * \param lodBias the lod bias which may be specified by a TXB instruction,
92 * otherwise zero.
Brian865f88a2006-12-14 15:14:14 -070093 */
94static void
95fetch_texel_deriv( GLcontext *ctx, const GLfloat texcoord[4],
96 const GLfloat texdx[4], const GLfloat texdy[4],
Brian999b5562007-11-23 12:01:57 -070097 GLfloat lodBias, GLuint unit, GLfloat color[4] )
Brian865f88a2006-12-14 15:14:14 -070098{
99 SWcontext *swrast = SWRAST_CONTEXT(ctx);
Brian Paul2acd5de2009-09-23 13:35:03 -0600100 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
101 const struct gl_texture_object *texObj = texUnit->_Current;
Brian865f88a2006-12-14 15:14:14 -0700102
Brian Paulc71fa342008-07-08 15:11:23 -0600103 if (texObj) {
Brian Paulbe1a76f2009-01-28 09:16:11 -0700104 const struct gl_texture_image *texImg =
105 texObj->Image[0][texObj->BaseLevel];
Brian Paulc71fa342008-07-08 15:11:23 -0600106 const GLfloat texW = (GLfloat) texImg->WidthScale;
107 const GLfloat texH = (GLfloat) texImg->HeightScale;
Brian Paulbe1a76f2009-01-28 09:16:11 -0700108 GLfloat lambda;
Brian Paulde2afd82009-03-08 13:49:57 -0600109 GLfloat rgba[4];
Brian865f88a2006-12-14 15:14:14 -0700110
Brian Paulc71fa342008-07-08 15:11:23 -0600111 lambda = _swrast_compute_lambda(texdx[0], texdy[0], /* ds/dx, ds/dy */
112 texdx[1], texdy[1], /* dt/dx, dt/dy */
Brian Paulad935c32009-09-23 12:54:14 -0600113 texdx[3], texdy[3], /* dq/dx, dq/dy */
Brian Paulc71fa342008-07-08 15:11:23 -0600114 texW, texH,
115 texcoord[0], texcoord[1], texcoord[3],
Brian Paul2acd5de2009-09-23 13:35:03 -0600116 1.0F / texcoord[3]);
117
118 lambda += lodBias + texUnit->LodBias + texObj->LodBias;
Brian Paulc71fa342008-07-08 15:11:23 -0600119
120 lambda = CLAMP(lambda, texObj->MinLod, texObj->MaxLod);
Brian999b5562007-11-23 12:01:57 -0700121
Brian Paulbe1a76f2009-01-28 09:16:11 -0700122 swrast->TextureSample[unit](ctx, texObj, 1,
123 (const GLfloat (*)[4]) texcoord,
124 &lambda, &rgba);
Brian Paul54c62ba2009-01-28 10:31:05 -0700125 swizzle_texel(rgba, color, texObj->_Swizzle);
Brian Paulbe1a76f2009-01-28 09:16:11 -0700126 }
127 else {
Brian Paul88f36562009-02-09 12:43:09 -0700128 ASSIGN_4V(color, 0.0F, 0.0F, 0.0F, 1.0F);
Brian Paulbe1a76f2009-01-28 09:16:11 -0700129 }
Brian865f88a2006-12-14 15:14:14 -0700130}
131
132
133/**
Brian865f88a2006-12-14 15:14:14 -0700134 * Initialize the virtual fragment program machine state prior to running
135 * fragment program on a fragment. This involves initializing the input
136 * registers, condition codes, etc.
137 * \param machine the virtual machine state to init
138 * \param program the fragment program we're about to run
139 * \param span the span of pixels we'll operate on
140 * \param col which element (column) of the span we'll operate on
141 */
142static void
Brianf6803de2007-02-22 16:08:01 -0700143init_machine(GLcontext *ctx, struct gl_program_machine *machine,
144 const struct gl_fragment_program *program,
145 const SWspan *span, GLuint col)
Brian865f88a2006-12-14 15:14:14 -0700146{
Eric Anholt11c581c2010-01-26 12:43:43 -0800147 GLfloat *wpos = span->array->attribs[FRAG_ATTRIB_WPOS][col];
148
Brianced6f762007-04-20 08:21:49 -0600149 if (program->Base.Target == GL_FRAGMENT_PROGRAM_NV) {
Brian865f88a2006-12-14 15:14:14 -0700150 /* Clear temporary registers (undefined for ARB_f_p) */
Brian Paul6bf1ea82010-02-19 08:32:36 -0700151 memset(machine->Temporaries, 0, MAX_PROGRAM_TEMPS * 4 * sizeof(GLfloat));
Brian865f88a2006-12-14 15:14:14 -0700152 }
153
Eric Anholt11c581c2010-01-26 12:43:43 -0800154 /* ARB_fragment_coord_conventions */
155 if (program->OriginUpperLeft)
156 wpos[1] = ctx->DrawBuffer->Height - 1 - wpos[1];
157 if (!program->PixelCenterInteger) {
Brian Paul880411c2010-01-27 17:00:32 -0700158 wpos[0] += 0.5F;
159 wpos[1] += 0.5F;
Eric Anholt11c581c2010-01-26 12:43:43 -0800160 }
161
Brianf3e507e2007-02-01 09:51:48 -0700162 /* Setup pointer to input attributes */
163 machine->Attribs = span->array->attribs;
Brian10b58952007-03-10 11:30:19 -0700164
Brian60d136f2007-05-02 18:45:44 -0600165 machine->DerivX = (GLfloat (*)[4]) span->attrStepX;
166 machine->DerivY = (GLfloat (*)[4]) span->attrStepY;
167 machine->NumDeriv = FRAG_ATTRIB_MAX;
168
Brian Paulade50832008-05-14 16:09:46 -0600169 machine->Samplers = program->Base.SamplerUnits;
170
Briane48f0b02007-10-24 11:37:05 -0600171 /* if running a GLSL program (not ARB_fragment_program) */
Brianaf0ae932007-04-28 08:51:23 -0600172 if (ctx->Shader.CurrentProgram) {
Brian Paul9d0b8d72009-07-29 20:07:41 -0600173 /* Store front/back facing value */
Brian Paul880411c2010-01-27 17:00:32 -0700174 machine->Attribs[FRAG_ATTRIB_FACE][col][0] = 1.0F - span->facing;
Brianaf0ae932007-04-28 08:51:23 -0600175 }
Brian10b58952007-03-10 11:30:19 -0700176
Brianf6803de2007-02-22 16:08:01 -0700177 machine->CurElement = col;
Brian865f88a2006-12-14 15:14:14 -0700178
179 /* init condition codes */
180 machine->CondCodes[0] = COND_EQ;
181 machine->CondCodes[1] = COND_EQ;
182 machine->CondCodes[2] = COND_EQ;
183 machine->CondCodes[3] = COND_EQ;
184
185 /* init call stack */
186 machine->StackDepth = 0;
Brianf6803de2007-02-22 16:08:01 -0700187
Brian Paulade50832008-05-14 16:09:46 -0600188 machine->FetchTexelLod = fetch_texel_lod;
Brianf6803de2007-02-22 16:08:01 -0700189 machine->FetchTexelDeriv = fetch_texel_deriv;
Brian865f88a2006-12-14 15:14:14 -0700190}
191
192
193/**
194 * Run fragment program on the pixels in span from 'start' to 'end' - 1.
195 */
196static void
197run_program(GLcontext *ctx, SWspan *span, GLuint start, GLuint end)
198{
Brian1c09bcf2007-03-11 17:00:39 -0600199 SWcontext *swrast = SWRAST_CONTEXT(ctx);
Brian865f88a2006-12-14 15:14:14 -0700200 const struct gl_fragment_program *program = ctx->FragmentProgram._Current;
Ian Romanick5606dfb2009-11-17 16:10:24 -0800201 const GLbitfield64 outputsWritten = program->Base.OutputsWritten;
Brian8b5fce62007-04-19 14:24:10 -0600202 struct gl_program_machine *machine = &swrast->FragProgMachine;
Brian865f88a2006-12-14 15:14:14 -0700203 GLuint i;
204
Brian865f88a2006-12-14 15:14:14 -0700205 for (i = start; i < end; i++) {
206 if (span->array->mask[i]) {
Brian8b5fce62007-04-19 14:24:10 -0600207 init_machine(ctx, machine, program, span, i);
Brian865f88a2006-12-14 15:14:14 -0700208
Brian8b5fce62007-04-19 14:24:10 -0600209 if (_mesa_execute_program(ctx, &program->Base, machine)) {
Brian1c09bcf2007-03-11 17:00:39 -0600210
Brian865f88a2006-12-14 15:14:14 -0700211 /* Store result color */
Ian Romanick5606dfb2009-11-17 16:10:24 -0800212 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_COLOR)) {
Brian1c09bcf2007-03-11 17:00:39 -0600213 COPY_4V(span->array->attribs[FRAG_ATTRIB_COL0][i],
Brian Paul8d475822009-02-28 11:49:46 -0700214 machine->Outputs[FRAG_RESULT_COLOR]);
Brian1c09bcf2007-03-11 17:00:39 -0600215 }
216 else {
217 /* Multiple drawbuffers / render targets
218 * Note that colors beyond 0 and 1 will overwrite other
219 * attributes, such as FOGC, TEX0, TEX1, etc. That's OK.
220 */
Brianff73c782008-01-06 10:43:20 -0700221 GLuint buf;
222 for (buf = 0; buf < ctx->DrawBuffer->_NumColorDrawBuffers; buf++) {
Ian Romanick5606dfb2009-11-17 16:10:24 -0800223 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_DATA0 + buf)) {
Brianff73c782008-01-06 10:43:20 -0700224 COPY_4V(span->array->attribs[FRAG_ATTRIB_COL0 + buf][i],
225 machine->Outputs[FRAG_RESULT_DATA0 + buf]);
Brian1c09bcf2007-03-11 17:00:39 -0600226 }
227 }
228 }
Brian865f88a2006-12-14 15:14:14 -0700229
230 /* Store result depth/z */
Ian Romanick5606dfb2009-11-17 16:10:24 -0800231 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
Brian Paul8d475822009-02-28 11:49:46 -0700232 const GLfloat depth = machine->Outputs[FRAG_RESULT_DEPTH][2];
Brian865f88a2006-12-14 15:14:14 -0700233 if (depth <= 0.0)
234 span->array->z[i] = 0;
235 else if (depth >= 1.0)
236 span->array->z[i] = ctx->DrawBuffer->_DepthMax;
237 else
238 span->array->z[i] = IROUND(depth * ctx->DrawBuffer->_DepthMaxF);
239 }
240 }
241 else {
242 /* killed fragment */
243 span->array->mask[i] = GL_FALSE;
244 span->writeAll = GL_FALSE;
245 }
246 }
247 }
Brian865f88a2006-12-14 15:14:14 -0700248}
249
250
251/**
252 * Execute the current fragment program for all the fragments
253 * in the given span.
254 */
255void
256_swrast_exec_fragment_program( GLcontext *ctx, SWspan *span )
257{
258 const struct gl_fragment_program *program = ctx->FragmentProgram._Current;
259
260 /* incoming colors should be floats */
Brian17ad1d12007-03-13 10:53:16 -0600261 if (program->Base.InputsRead & FRAG_BIT_COL0) {
262 ASSERT(span->array->ChanType == GL_FLOAT);
263 }
Brian865f88a2006-12-14 15:14:14 -0700264
Brian865f88a2006-12-14 15:14:14 -0700265 run_program(ctx, span, 0, span->end);
266
Ian Romanick5606dfb2009-11-17 16:10:24 -0800267 if (program->Base.OutputsWritten & BITFIELD64_BIT(FRAG_RESULT_COLOR)) {
Brian17ad1d12007-03-13 10:53:16 -0600268 span->interpMask &= ~SPAN_RGBA;
269 span->arrayMask |= SPAN_RGBA;
270 }
271
Ian Romanick5606dfb2009-11-17 16:10:24 -0800272 if (program->Base.OutputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
Brian865f88a2006-12-14 15:14:14 -0700273 span->interpMask &= ~SPAN_Z;
274 span->arrayMask |= SPAN_Z;
275 }
Brian865f88a2006-12-14 15:14:14 -0700276}
277