2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
25 #include "main/glheader.h"
26 #include "main/colormac.h"
27 #include "program/prog_instruction.h"
29 #include "s_context.h"
30 #include "s_fragprog.h"
34 * \brief Should swrast use a fragment program?
36 * \return true if the current fragment program exists and is not the fixed
37 * function fragment program
40 _swrast_use_fragment_program(struct gl_context
*ctx
)
42 struct gl_fragment_program
*fp
= ctx
->FragmentProgram
._Current
;
43 return fp
&& !(fp
== ctx
->FragmentProgram
._TexEnvProgram
44 && fp
->Base
.NumInstructions
== 0);
48 * Apply texture object's swizzle (X/Y/Z/W/0/1) to incoming 'texel'
49 * and return results in 'colorOut'.
52 swizzle_texel(const GLfloat texel
[4], GLfloat colorOut
[4], GLuint swizzle
)
54 if (swizzle
== SWIZZLE_NOOP
) {
55 COPY_4V(colorOut
, texel
);
59 vector
[SWIZZLE_X
] = texel
[0];
60 vector
[SWIZZLE_Y
] = texel
[1];
61 vector
[SWIZZLE_Z
] = texel
[2];
62 vector
[SWIZZLE_W
] = texel
[3];
63 vector
[SWIZZLE_ZERO
] = 0.0F
;
64 vector
[SWIZZLE_ONE
] = 1.0F
;
65 colorOut
[0] = vector
[GET_SWZ(swizzle
, 0)];
66 colorOut
[1] = vector
[GET_SWZ(swizzle
, 1)];
67 colorOut
[2] = vector
[GET_SWZ(swizzle
, 2)];
68 colorOut
[3] = vector
[GET_SWZ(swizzle
, 3)];
74 * Fetch a texel with given lod.
75 * Called via machine->FetchTexelLod()
78 fetch_texel_lod( struct gl_context
*ctx
, const GLfloat texcoord
[4], GLfloat lambda
,
79 GLuint unit
, GLfloat color
[4] )
81 const struct gl_texture_object
*texObj
= ctx
->Texture
.Unit
[unit
]._Current
;
84 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
87 lambda
= CLAMP(lambda
, texObj
->Sampler
.MinLod
, texObj
->Sampler
.MaxLod
);
89 swrast
->TextureSample
[unit
](ctx
, texObj
, 1,
90 (const GLfloat (*)[4]) texcoord
,
92 swizzle_texel(rgba
, color
, texObj
->_Swizzle
);
95 ASSIGN_4V(color
, 0.0F
, 0.0F
, 0.0F
, 1.0F
);
101 * Fetch a texel with the given partial derivatives to compute a level
102 * of detail in the mipmap.
103 * Called via machine->FetchTexelDeriv()
104 * \param lodBias the lod bias which may be specified by a TXB instruction,
108 fetch_texel_deriv( struct gl_context
*ctx
, const GLfloat texcoord
[4],
109 const GLfloat texdx
[4], const GLfloat texdy
[4],
110 GLfloat lodBias
, GLuint unit
, GLfloat color
[4] )
112 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
113 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
114 const struct gl_texture_object
*texObj
= texUnit
->_Current
;
117 const struct gl_texture_image
*texImg
=
118 texObj
->Image
[0][texObj
->BaseLevel
];
119 const struct swrast_texture_image
*swImg
=
120 swrast_texture_image_const(texImg
);
121 const GLfloat texW
= (GLfloat
) swImg
->WidthScale
;
122 const GLfloat texH
= (GLfloat
) swImg
->HeightScale
;
126 lambda
= _swrast_compute_lambda(texdx
[0], texdy
[0], /* ds/dx, ds/dy */
127 texdx
[1], texdy
[1], /* dt/dx, dt/dy */
128 texdx
[3], texdy
[3], /* dq/dx, dq/dy */
130 texcoord
[0], texcoord
[1], texcoord
[3],
133 lambda
+= lodBias
+ texUnit
->LodBias
+ texObj
->Sampler
.LodBias
;
135 lambda
= CLAMP(lambda
, texObj
->Sampler
.MinLod
, texObj
->Sampler
.MaxLod
);
137 swrast
->TextureSample
[unit
](ctx
, texObj
, 1,
138 (const GLfloat (*)[4]) texcoord
,
140 swizzle_texel(rgba
, color
, texObj
->_Swizzle
);
143 ASSIGN_4V(color
, 0.0F
, 0.0F
, 0.0F
, 1.0F
);
149 * Initialize the virtual fragment program machine state prior to running
150 * fragment program on a fragment. This involves initializing the input
151 * registers, condition codes, etc.
152 * \param machine the virtual machine state to init
153 * \param program the fragment program we're about to run
154 * \param span the span of pixels we'll operate on
155 * \param col which element (column) of the span we'll operate on
158 init_machine(struct gl_context
*ctx
, struct gl_program_machine
*machine
,
159 const struct gl_fragment_program
*program
,
160 const SWspan
*span
, GLuint col
)
162 GLfloat
*wpos
= span
->array
->attribs
[FRAG_ATTRIB_WPOS
][col
];
164 if (program
->Base
.Target
== GL_FRAGMENT_PROGRAM_NV
) {
165 /* Clear temporary registers (undefined for ARB_f_p) */
166 memset(machine
->Temporaries
, 0, MAX_PROGRAM_TEMPS
* 4 * sizeof(GLfloat
));
169 /* ARB_fragment_coord_conventions */
170 if (program
->OriginUpperLeft
)
171 wpos
[1] = ctx
->DrawBuffer
->Height
- 1 - wpos
[1];
172 if (!program
->PixelCenterInteger
) {
177 /* Setup pointer to input attributes */
178 machine
->Attribs
= span
->array
->attribs
;
180 machine
->DerivX
= (GLfloat (*)[4]) span
->attrStepX
;
181 machine
->DerivY
= (GLfloat (*)[4]) span
->attrStepY
;
182 machine
->NumDeriv
= FRAG_ATTRIB_MAX
;
184 machine
->Samplers
= program
->Base
.SamplerUnits
;
186 /* if running a GLSL program (not ARB_fragment_program) */
187 if (ctx
->Shader
.CurrentFragmentProgram
) {
188 /* Store front/back facing value */
189 machine
->Attribs
[FRAG_ATTRIB_FACE
][col
][0] = 1.0F
- span
->facing
;
192 machine
->CurElement
= col
;
194 /* init condition codes */
195 machine
->CondCodes
[0] = COND_EQ
;
196 machine
->CondCodes
[1] = COND_EQ
;
197 machine
->CondCodes
[2] = COND_EQ
;
198 machine
->CondCodes
[3] = COND_EQ
;
200 /* init call stack */
201 machine
->StackDepth
= 0;
203 machine
->FetchTexelLod
= fetch_texel_lod
;
204 machine
->FetchTexelDeriv
= fetch_texel_deriv
;
209 * Run fragment program on the pixels in span from 'start' to 'end' - 1.
212 run_program(struct gl_context
*ctx
, SWspan
*span
, GLuint start
, GLuint end
)
214 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
215 const struct gl_fragment_program
*program
= ctx
->FragmentProgram
._Current
;
216 const GLbitfield64 outputsWritten
= program
->Base
.OutputsWritten
;
217 struct gl_program_machine
*machine
= &swrast
->FragProgMachine
;
220 for (i
= start
; i
< end
; i
++) {
221 if (span
->array
->mask
[i
]) {
222 init_machine(ctx
, machine
, program
, span
, i
);
224 if (_mesa_execute_program(ctx
, &program
->Base
, machine
)) {
226 /* Store result color */
227 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_COLOR
)) {
228 COPY_4V(span
->array
->attribs
[FRAG_ATTRIB_COL0
][i
],
229 machine
->Outputs
[FRAG_RESULT_COLOR
]);
232 /* Multiple drawbuffers / render targets
233 * Note that colors beyond 0 and 1 will overwrite other
234 * attributes, such as FOGC, TEX0, TEX1, etc. That's OK.
237 for (buf
= 0; buf
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; buf
++) {
238 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DATA0
+ buf
)) {
239 COPY_4V(span
->array
->attribs
[FRAG_ATTRIB_COL0
+ buf
][i
],
240 machine
->Outputs
[FRAG_RESULT_DATA0
+ buf
]);
245 /* Store result depth/z */
246 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
247 const GLfloat depth
= machine
->Outputs
[FRAG_RESULT_DEPTH
][2];
249 span
->array
->z
[i
] = 0;
250 else if (depth
>= 1.0)
251 span
->array
->z
[i
] = ctx
->DrawBuffer
->_DepthMax
;
254 (GLuint
) (depth
* ctx
->DrawBuffer
->_DepthMaxF
+ 0.5F
);
258 /* killed fragment */
259 span
->array
->mask
[i
] = GL_FALSE
;
260 span
->writeAll
= GL_FALSE
;
268 * Execute the current fragment program for all the fragments
272 _swrast_exec_fragment_program( struct gl_context
*ctx
, SWspan
*span
)
274 const struct gl_fragment_program
*program
= ctx
->FragmentProgram
._Current
;
276 /* incoming colors should be floats */
277 if (program
->Base
.InputsRead
& FRAG_BIT_COL0
) {
278 ASSERT(span
->array
->ChanType
== GL_FLOAT
);
281 run_program(ctx
, span
, 0, span
->end
);
283 if (program
->Base
.OutputsWritten
& BITFIELD64_BIT(FRAG_RESULT_COLOR
)) {
284 span
->interpMask
&= ~SPAN_RGBA
;
285 span
->arrayMask
|= SPAN_RGBA
;
288 if (program
->Base
.OutputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
289 span
->interpMask
&= ~SPAN_Z
;
290 span
->arrayMask
|= SPAN_Z
;