blob: 093bc6651a98fec46ba4381e99cf8e7a1a68df8f [file] [log] [blame]
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001/**************************************************************************
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07002 *
Keith Whitwell32ef6e72008-09-20 08:26:11 -07003 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07005 *
Keith Whitwell32ef6e72008-09-20 08:26:11 -07006 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
Keith Whitwell6dfb89e2009-05-31 19:04:06 -070013 *
Keith Whitwell32ef6e72008-09-20 08:26:11 -070014 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
Keith Whitwell6dfb89e2009-05-31 19:04:06 -070017 *
Keith Whitwell32ef6e72008-09-20 08:26:11 -070018 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Keith Whitwell6dfb89e2009-05-31 19:04:06 -070025 *
Keith Whitwell32ef6e72008-09-20 08:26:11 -070026 **************************************************************************/
27
28/**
Brian Paul1d03f282008-12-31 16:38:10 -070029 * \file ffvertex_prog.c
Keith Whitwell32ef6e72008-09-20 08:26:11 -070030 *
31 * Create a vertex program to execute the current fixed function T&L pipeline.
32 * \author Keith Whitwell
33 */
34
35
36#include "main/glheader.h"
37#include "main/mtypes.h"
38#include "main/macros.h"
Vinson Leedb61b9c2011-01-07 00:08:24 -080039#include "main/mfeatures.h"
Keith Whitwell32ef6e72008-09-20 08:26:11 -070040#include "main/enums.h"
41#include "main/ffvertex_prog.h"
Brian Paulec2b92f2010-06-10 23:02:41 -060042#include "program/program.h"
43#include "program/prog_cache.h"
44#include "program/prog_instruction.h"
45#include "program/prog_parameter.h"
46#include "program/prog_print.h"
47#include "program/prog_statevars.h"
Keith Whitwell32ef6e72008-09-20 08:26:11 -070048
49
Brian Paula7a3d3c2009-09-02 09:43:00 -060050/** Max of number of lights and texture coord units */
51#define NUM_UNITS MAX2(MAX_TEXTURE_COORD_UNITS, MAX_LIGHTS)
52
Keith Whitwell32ef6e72008-09-20 08:26:11 -070053struct state_key {
Keith Whitwell568e96b2008-10-14 14:15:26 +010054 unsigned light_color_material_mask:12;
Keith Whitwell32ef6e72008-09-20 08:26:11 -070055 unsigned light_global_enabled:1;
56 unsigned light_local_viewer:1;
57 unsigned light_twoside:1;
Keith Whitwell32ef6e72008-09-20 08:26:11 -070058 unsigned material_shininess_is_zero:1;
Keith Whitwell32ef6e72008-09-20 08:26:11 -070059 unsigned need_eye_coords:1;
60 unsigned normalize:1;
61 unsigned rescale_normals:1;
Keith Whitwell568e96b2008-10-14 14:15:26 +010062
Keith Whitwell32ef6e72008-09-20 08:26:11 -070063 unsigned fog_source_is_depth:1;
Nicholas Miellfadd5e12011-09-19 23:17:00 -070064 unsigned fog_distance_mode:2;
Keith Whitwell32ef6e72008-09-20 08:26:11 -070065 unsigned separate_specular:1;
Keith Whitwell32ef6e72008-09-20 08:26:11 -070066 unsigned point_attenuated:1;
67 unsigned point_array:1;
68 unsigned texture_enabled_global:1;
69 unsigned fragprog_inputs_read:12;
70
Mathias Fröhlichdca6a282011-10-31 18:32:59 +010071 GLbitfield64 varying_vp_inputs;
Keith Whitwell568e96b2008-10-14 14:15:26 +010072
Keith Whitwell32ef6e72008-09-20 08:26:11 -070073 struct {
74 unsigned light_enabled:1;
75 unsigned light_eyepos3_is_zero:1;
76 unsigned light_spotcutoff_is_180:1;
Keith Whitwell6dfb89e2009-05-31 19:04:06 -070077 unsigned light_attenuated:1;
Keith Whitwell32ef6e72008-09-20 08:26:11 -070078 unsigned texunit_really_enabled:1;
79 unsigned texmat_enabled:1;
Dave Airlie911fa4a2010-04-12 15:40:29 +100080 unsigned coord_replace:1;
Keith Whitwell32ef6e72008-09-20 08:26:11 -070081 unsigned texgen_enabled:4;
82 unsigned texgen_mode0:4;
83 unsigned texgen_mode1:4;
84 unsigned texgen_mode2:4;
85 unsigned texgen_mode3:4;
Brian Paula7a3d3c2009-09-02 09:43:00 -060086 } unit[NUM_UNITS];
Keith Whitwell32ef6e72008-09-20 08:26:11 -070087};
88
89
Keith Whitwell32ef6e72008-09-20 08:26:11 -070090#define TXG_NONE 0
91#define TXG_OBJ_LINEAR 1
92#define TXG_EYE_LINEAR 2
93#define TXG_SPHERE_MAP 3
94#define TXG_REFLECTION_MAP 4
95#define TXG_NORMAL_MAP 5
96
97static GLuint translate_texgen( GLboolean enabled, GLenum mode )
98{
99 if (!enabled)
100 return TXG_NONE;
101
102 switch (mode) {
103 case GL_OBJECT_LINEAR: return TXG_OBJ_LINEAR;
104 case GL_EYE_LINEAR: return TXG_EYE_LINEAR;
105 case GL_SPHERE_MAP: return TXG_SPHERE_MAP;
106 case GL_REFLECTION_MAP_NV: return TXG_REFLECTION_MAP;
107 case GL_NORMAL_MAP_NV: return TXG_NORMAL_MAP;
108 default: return TXG_NONE;
109 }
110}
111
Nicholas Miellfadd5e12011-09-19 23:17:00 -0700112#define FDM_EYE_RADIAL 0
113#define FDM_EYE_PLANE 1
114#define FDM_EYE_PLANE_ABS 2
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700115
Nicholas Miellfadd5e12011-09-19 23:17:00 -0700116static GLuint translate_fog_distance_mode( GLenum mode )
117{
118 switch (mode) {
119 case GL_EYE_RADIAL_NV:
120 return FDM_EYE_RADIAL;
121 case GL_EYE_PLANE:
122 return FDM_EYE_PLANE;
123 default: /* shouldn't happen; fall through to a sensible default */
124 case GL_EYE_PLANE_ABSOLUTE_NV:
125 return FDM_EYE_PLANE_ABS;
126 }
127}
Brian Paul1d03f282008-12-31 16:38:10 -0700128
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400129static GLboolean check_active_shininess( struct gl_context *ctx,
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700130 const struct state_key *key,
131 GLuint side )
132{
Mathias Fröhlichdca6a282011-10-31 18:32:59 +0100133 GLuint attr = MAT_ATTRIB_FRONT_SHININESS + side;
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700134
Keith Whitwellf6c8ca02009-05-31 19:01:55 -0700135 if ((key->varying_vp_inputs & VERT_BIT_COLOR0) &&
Mathias Fröhlichdca6a282011-10-31 18:32:59 +0100136 (key->light_color_material_mask & (1 << attr)))
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700137 return GL_TRUE;
138
Mathias Fröhlichdca6a282011-10-31 18:32:59 +0100139 if (key->varying_vp_inputs & VERT_ATTRIB_GENERIC(attr))
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700140 return GL_TRUE;
141
Mathias Fröhlichdca6a282011-10-31 18:32:59 +0100142 if (ctx->Light.Material.Attrib[attr][0] != 0.0F)
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700143 return GL_TRUE;
144
145 return GL_FALSE;
146}
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700147
148
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400149static void make_state_key( struct gl_context *ctx, struct state_key *key )
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700150{
151 const struct gl_fragment_program *fp;
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700152 GLuint i;
153
Shunichi Fujie095d582008-09-26 18:55:13 +0900154 memset(key, 0, sizeof(struct state_key));
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700155 fp = ctx->FragmentProgram._Current;
156
157 /* This now relies on texenvprogram.c being active:
158 */
159 assert(fp);
160
161 key->need_eye_coords = ctx->_NeedEyeCoords;
162
163 key->fragprog_inputs_read = fp->Base.InputsRead;
Keith Whitwell568e96b2008-10-14 14:15:26 +0100164 key->varying_vp_inputs = ctx->varying_vp_inputs;
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700165
166 if (ctx->RenderMode == GL_FEEDBACK) {
167 /* make sure the vertprog emits color and tex0 */
Paul Berryeed6baf2013-02-23 09:00:58 -0800168 key->fragprog_inputs_read |= (VARYING_BIT_COL0 | VARYING_BIT_TEX0);
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700169 }
170
171 key->separate_specular = (ctx->Light.Model.ColorControl ==
172 GL_SEPARATE_SPECULAR_COLOR);
173
174 if (ctx->Light.Enabled) {
175 key->light_global_enabled = 1;
176
177 if (ctx->Light.Model.LocalViewer)
178 key->light_local_viewer = 1;
179
180 if (ctx->Light.Model.TwoSide)
181 key->light_twoside = 1;
182
183 if (ctx->Light.ColorMaterialEnabled) {
Brian Paul4cb35792012-06-26 14:47:19 -0600184 key->light_color_material_mask = ctx->Light._ColorMaterialBitmask;
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700185 }
186
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700187 for (i = 0; i < MAX_LIGHTS; i++) {
188 struct gl_light *light = &ctx->Light.Light[i];
189
190 if (light->Enabled) {
191 key->unit[i].light_enabled = 1;
192
193 if (light->EyePosition[3] == 0.0)
194 key->unit[i].light_eyepos3_is_zero = 1;
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700195
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700196 if (light->SpotCutoff == 180.0)
197 key->unit[i].light_spotcutoff_is_180 = 1;
198
199 if (light->ConstantAttenuation != 1.0 ||
200 light->LinearAttenuation != 0.0 ||
201 light->QuadraticAttenuation != 0.0)
202 key->unit[i].light_attenuated = 1;
203 }
204 }
205
206 if (check_active_shininess(ctx, key, 0)) {
207 key->material_shininess_is_zero = 0;
208 }
209 else if (key->light_twoside &&
210 check_active_shininess(ctx, key, 1)) {
211 key->material_shininess_is_zero = 0;
212 }
213 else {
214 key->material_shininess_is_zero = 1;
215 }
216 }
217
218 if (ctx->Transform.Normalize)
219 key->normalize = 1;
220
221 if (ctx->Transform.RescaleNormals)
222 key->rescale_normals = 1;
223
Nicholas Miellfadd5e12011-09-19 23:17:00 -0700224 if (ctx->Fog.FogCoordinateSource == GL_FRAGMENT_DEPTH_EXT) {
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700225 key->fog_source_is_depth = 1;
Nicholas Miellfadd5e12011-09-19 23:17:00 -0700226 key->fog_distance_mode = translate_fog_distance_mode(ctx->Fog.FogDistanceMode);
227 }
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700228
229 if (ctx->Point._Attenuated)
230 key->point_attenuated = 1;
231
Chia-I Wu76ba4312011-11-29 15:19:28 +0800232 if (ctx->Array.ArrayObj->VertexAttrib[VERT_ATTRIB_POINT_SIZE].Enabled)
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700233 key->point_array = 1;
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700234
235 if (ctx->Texture._TexGenEnabled ||
236 ctx->Texture._TexMatEnabled ||
237 ctx->Texture._EnabledUnits)
238 key->texture_enabled_global = 1;
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700239
Brian Paule9b34882008-12-31 11:54:02 -0700240 for (i = 0; i < MAX_TEXTURE_COORD_UNITS; i++) {
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700241 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
242
243 if (texUnit->_ReallyEnabled)
244 key->unit[i].texunit_really_enabled = 1;
245
Dave Airlie911fa4a2010-04-12 15:40:29 +1000246 if (ctx->Point.PointSprite)
247 if (ctx->Point.CoordReplace[i])
248 key->unit[i].coord_replace = 1;
249
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700250 if (ctx->Texture._TexMatEnabled & ENABLE_TEXMAT(i))
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700251 key->unit[i].texmat_enabled = 1;
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700252
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700253 if (texUnit->TexGenEnabled) {
254 key->unit[i].texgen_enabled = 1;
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700255
256 key->unit[i].texgen_mode0 =
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700257 translate_texgen( texUnit->TexGenEnabled & (1<<0),
Brian Paul9705cff2009-02-21 13:23:04 -0700258 texUnit->GenS.Mode );
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700259 key->unit[i].texgen_mode1 =
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700260 translate_texgen( texUnit->TexGenEnabled & (1<<1),
Brian Paul9705cff2009-02-21 13:23:04 -0700261 texUnit->GenT.Mode );
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700262 key->unit[i].texgen_mode2 =
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700263 translate_texgen( texUnit->TexGenEnabled & (1<<2),
Brian Paul9705cff2009-02-21 13:23:04 -0700264 texUnit->GenR.Mode );
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700265 key->unit[i].texgen_mode3 =
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700266 translate_texgen( texUnit->TexGenEnabled & (1<<3),
Brian Paul9705cff2009-02-21 13:23:04 -0700267 texUnit->GenQ.Mode );
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700268 }
269 }
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700270}
271
272
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700273
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700274/* Very useful debugging tool - produces annotated listing of
275 * generated program with line/function references for each
276 * instruction back into this file:
277 */
278#define DISASSEM 0
279
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700280
281/* Use uregs to represent registers internally, translate to Mesa's
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700282 * expected formats on emit.
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700283 *
284 * NOTE: These are passed by value extensively in this file rather
285 * than as usual by pointer reference. If this disturbs you, try
286 * remembering they are just 32bits in size.
287 *
288 * GCC is smart enough to deal with these dword-sized structures in
289 * much the same way as if I had defined them as dwords and was using
290 * macros to access and set the fields. This is much nicer and easier
291 * to evolve.
292 */
293struct ureg {
294 GLuint file:4;
295 GLint idx:9; /* relative addressing may be negative */
296 /* sizeof(idx) should == sizeof(prog_src_reg::Index) */
297 GLuint negate:1;
298 GLuint swz:12;
299 GLuint pad:6;
300};
301
302
303struct tnl_program {
304 const struct state_key *state;
305 struct gl_vertex_program *program;
306 GLint max_inst; /** number of instructions allocated for program */
Keith Whitwellb6e82562009-05-05 12:12:28 +0100307 GLboolean mvp_with_dp4;
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700308
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700309 GLuint temp_in_use;
310 GLuint temp_reserved;
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700311
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700312 struct ureg eye_position;
313 struct ureg eye_position_z;
314 struct ureg eye_position_normalized;
315 struct ureg transformed_normal;
316 struct ureg identity;
317
318 GLuint materials;
319 GLuint color_materials;
320};
321
322
Brian Paul53d50752009-03-31 15:47:05 -0600323static const struct ureg undef = {
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700324 PROGRAM_UNDEFINED,
325 0,
326 0,
327 0,
328 0
329};
330
331/* Local shorthand:
332 */
333#define X SWIZZLE_X
334#define Y SWIZZLE_Y
335#define Z SWIZZLE_Z
336#define W SWIZZLE_W
337
338
339/* Construct a ureg:
340 */
341static struct ureg make_ureg(GLuint file, GLint idx)
342{
343 struct ureg reg;
344 reg.file = file;
345 reg.idx = idx;
346 reg.negate = 0;
347 reg.swz = SWIZZLE_NOOP;
348 reg.pad = 0;
349 return reg;
350}
351
352
353
354static struct ureg negate( struct ureg reg )
355{
356 reg.negate ^= 1;
357 return reg;
Brian Paul53d50752009-03-31 15:47:05 -0600358}
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700359
360
361static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
362{
363 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
364 GET_SWZ(reg.swz, y),
365 GET_SWZ(reg.swz, z),
366 GET_SWZ(reg.swz, w));
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700367 return reg;
368}
369
Brian Paul1d03f282008-12-31 16:38:10 -0700370
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700371static struct ureg swizzle1( struct ureg reg, int x )
372{
373 return swizzle(reg, x, x, x, x);
374}
375
Brian Paul1d03f282008-12-31 16:38:10 -0700376
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700377static struct ureg get_temp( struct tnl_program *p )
378{
Brian Paul9a548c22012-01-12 07:30:48 -0700379 int bit = ffs( ~p->temp_in_use );
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700380 if (!bit) {
381 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Eric Anholt1a80fe42009-12-22 10:56:34 -0800382 exit(1);
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700383 }
384
385 if ((GLuint) bit > p->program->Base.NumTemporaries)
386 p->program->Base.NumTemporaries = bit;
387
388 p->temp_in_use |= 1<<(bit-1);
389 return make_ureg(PROGRAM_TEMPORARY, bit-1);
390}
391
Brian Paul1d03f282008-12-31 16:38:10 -0700392
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700393static struct ureg reserve_temp( struct tnl_program *p )
394{
395 struct ureg temp = get_temp( p );
396 p->temp_reserved |= 1<<temp.idx;
397 return temp;
398}
399
Brian Paul1d03f282008-12-31 16:38:10 -0700400
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700401static void release_temp( struct tnl_program *p, struct ureg reg )
402{
403 if (reg.file == PROGRAM_TEMPORARY) {
404 p->temp_in_use &= ~(1<<reg.idx);
405 p->temp_in_use |= p->temp_reserved; /* can't release reserved temps */
406 }
407}
408
409static void release_temps( struct tnl_program *p )
410{
411 p->temp_in_use = p->temp_reserved;
412}
413
414
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700415static struct ureg register_param5(struct tnl_program *p,
Keith Whitwell568e96b2008-10-14 14:15:26 +0100416 GLint s0,
417 GLint s1,
418 GLint s2,
419 GLint s3,
420 GLint s4)
421{
422 gl_state_index tokens[STATE_LENGTH];
423 GLint idx;
424 tokens[0] = s0;
425 tokens[1] = s1;
426 tokens[2] = s2;
427 tokens[3] = s3;
428 tokens[4] = s4;
429 idx = _mesa_add_state_reference( p->program->Base.Parameters, tokens );
430 return make_ureg(PROGRAM_STATE_VAR, idx);
431}
432
433
434#define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
435#define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
436#define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
437#define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
438
439
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700440
441/**
442 * \param input one of VERT_ATTRIB_x tokens.
443 */
444static struct ureg register_input( struct tnl_program *p, GLuint input )
445{
Mathias Fröhlichdca6a282011-10-31 18:32:59 +0100446 assert(input < VERT_ATTRIB_MAX);
Keith Whitwellf6c8ca02009-05-31 19:01:55 -0700447
Mathias Fröhlichdca6a282011-10-31 18:32:59 +0100448 if (p->state->varying_vp_inputs & VERT_BIT(input)) {
449 p->program->Base.InputsRead |= VERT_BIT(input);
Keith Whitwell568e96b2008-10-14 14:15:26 +0100450 return make_ureg(PROGRAM_INPUT, input);
451 }
452 else {
453 return register_param3( p, STATE_INTERNAL, STATE_CURRENT_ATTRIB, input );
454 }
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700455}
456
Brian Paul1d03f282008-12-31 16:38:10 -0700457
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700458/**
Paul Berry36b252e2013-02-23 07:22:01 -0800459 * \param input one of VARYING_SLOT_x tokens.
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700460 */
461static struct ureg register_output( struct tnl_program *p, GLuint output )
462{
Ian Romanick5606dfb2009-11-17 16:10:24 -0800463 p->program->Base.OutputsWritten |= BITFIELD64_BIT(output);
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700464 return make_ureg(PROGRAM_OUTPUT, output);
465}
466
Brian Paul1d03f282008-12-31 16:38:10 -0700467
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700468static struct ureg register_const4f( struct tnl_program *p,
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700469 GLfloat s0,
470 GLfloat s1,
471 GLfloat s2,
472 GLfloat s3)
473{
Bryan Cain6d89aba2011-05-17 17:13:20 -0500474 gl_constant_value values[4];
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700475 GLint idx;
476 GLuint swizzle;
Bryan Cain6d89aba2011-05-17 17:13:20 -0500477 values[0].f = s0;
478 values[1].f = s1;
479 values[2].f = s2;
480 values[3].f = s3;
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700481 idx = _mesa_add_unnamed_constant( p->program->Base.Parameters, values, 4,
482 &swizzle );
483 ASSERT(swizzle == SWIZZLE_NOOP);
484 return make_ureg(PROGRAM_CONSTANT, idx);
485}
486
487#define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
488#define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
489#define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
490#define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
491
492static GLboolean is_undef( struct ureg reg )
493{
494 return reg.file == PROGRAM_UNDEFINED;
495}
496
Brian Paul1d03f282008-12-31 16:38:10 -0700497
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700498static struct ureg get_identity_param( struct tnl_program *p )
499{
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700500 if (is_undef(p->identity))
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700501 p->identity = register_const4f(p, 0,0,0,1);
502
503 return p->identity;
504}
505
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700506static void register_matrix_param5( struct tnl_program *p,
507 GLint s0, /* modelview, projection, etc */
508 GLint s1, /* texture matrix number */
509 GLint s2, /* first row */
510 GLint s3, /* last row */
511 GLint s4, /* inverse, transpose, etc */
512 struct ureg *matrix )
513{
514 GLint i;
515
516 /* This is a bit sad as the support is there to pull the whole
517 * matrix out in one go:
518 */
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700519 for (i = 0; i <= s3 - s2; i++)
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700520 matrix[i] = register_param5( p, s0, s1, i, i, s4 );
521}
522
523
524static void emit_arg( struct prog_src_register *src,
525 struct ureg reg )
526{
527 src->File = reg.file;
528 src->Index = reg.idx;
529 src->Swizzle = reg.swz;
Brian Paul7db7ff82009-04-14 22:14:30 -0600530 src->Negate = reg.negate ? NEGATE_XYZW : NEGATE_NONE;
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700531 src->Abs = 0;
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700532 src->RelAddr = 0;
533 /* Check that bitfield sizes aren't exceeded */
534 ASSERT(src->Index == reg.idx);
535}
536
Brian Paul1d03f282008-12-31 16:38:10 -0700537
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700538static void emit_dst( struct prog_dst_register *dst,
539 struct ureg reg, GLuint mask )
540{
541 dst->File = reg.file;
542 dst->Index = reg.idx;
543 /* allow zero as a shorthand for xyzw */
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700544 dst->WriteMask = mask ? mask : WRITEMASK_XYZW;
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700545 dst->CondMask = COND_TR; /* always pass cond test */
546 dst->CondSwizzle = SWIZZLE_NOOP;
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700547 /* Check that bitfield sizes aren't exceeded */
548 ASSERT(dst->Index == reg.idx);
549}
550
Brian Paul1d03f282008-12-31 16:38:10 -0700551
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700552static void debug_insn( struct prog_instruction *inst, const char *fn,
553 GLuint line )
554{
555 if (DISASSEM) {
556 static const char *last_fn;
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700557
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700558 if (fn != last_fn) {
559 last_fn = fn;
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500560 printf("%s:\n", fn);
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700561 }
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700562
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500563 printf("%d:\t", line);
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700564 _mesa_print_instruction(inst);
565 }
566}
567
568
569static void emit_op3fn(struct tnl_program *p,
570 enum prog_opcode op,
571 struct ureg dest,
572 GLuint mask,
573 struct ureg src0,
574 struct ureg src1,
575 struct ureg src2,
576 const char *fn,
577 GLuint line)
578{
579 GLuint nr;
580 struct prog_instruction *inst;
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700581
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700582 assert((GLint) p->program->Base.NumInstructions <= p->max_inst);
583
584 if (p->program->Base.NumInstructions == p->max_inst) {
585 /* need to extend the program's instruction array */
586 struct prog_instruction *newInst;
587
588 /* double the size */
589 p->max_inst *= 2;
590
591 newInst = _mesa_alloc_instructions(p->max_inst);
592 if (!newInst) {
593 _mesa_error(NULL, GL_OUT_OF_MEMORY, "vertex program build");
594 return;
595 }
596
597 _mesa_copy_instructions(newInst,
598 p->program->Base.Instructions,
599 p->program->Base.NumInstructions);
600
601 _mesa_free_instructions(p->program->Base.Instructions,
602 p->program->Base.NumInstructions);
603
604 p->program->Base.Instructions = newInst;
605 }
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700606
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700607 nr = p->program->Base.NumInstructions++;
608
609 inst = &p->program->Base.Instructions[nr];
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700610 inst->Opcode = (enum prog_opcode) op;
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700611 inst->Data = 0;
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700612
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700613 emit_arg( &inst->SrcReg[0], src0 );
614 emit_arg( &inst->SrcReg[1], src1 );
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700615 emit_arg( &inst->SrcReg[2], src2 );
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700616
617 emit_dst( &inst->DstReg, dest, mask );
618
619 debug_insn(inst, fn, line);
620}
621
622
623#define emit_op3(p, op, dst, mask, src0, src1, src2) \
624 emit_op3fn(p, op, dst, mask, src0, src1, src2, __FUNCTION__, __LINE__)
625
626#define emit_op2(p, op, dst, mask, src0, src1) \
627 emit_op3fn(p, op, dst, mask, src0, src1, undef, __FUNCTION__, __LINE__)
628
629#define emit_op1(p, op, dst, mask, src0) \
630 emit_op3fn(p, op, dst, mask, src0, undef, undef, __FUNCTION__, __LINE__)
631
632
633static struct ureg make_temp( struct tnl_program *p, struct ureg reg )
634{
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700635 if (reg.file == PROGRAM_TEMPORARY &&
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700636 !(p->temp_reserved & (1<<reg.idx)))
637 return reg;
638 else {
639 struct ureg temp = get_temp(p);
640 emit_op1(p, OPCODE_MOV, temp, 0, reg);
641 return temp;
642 }
643}
644
645
646/* Currently no tracking performed of input/output/register size or
647 * active elements. Could be used to reduce these operations, as
648 * could the matrix type.
649 */
650static void emit_matrix_transform_vec4( struct tnl_program *p,
651 struct ureg dest,
652 const struct ureg *mat,
653 struct ureg src)
654{
655 emit_op2(p, OPCODE_DP4, dest, WRITEMASK_X, src, mat[0]);
656 emit_op2(p, OPCODE_DP4, dest, WRITEMASK_Y, src, mat[1]);
657 emit_op2(p, OPCODE_DP4, dest, WRITEMASK_Z, src, mat[2]);
658 emit_op2(p, OPCODE_DP4, dest, WRITEMASK_W, src, mat[3]);
659}
660
Brian Paul1d03f282008-12-31 16:38:10 -0700661
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700662/* This version is much easier to implement if writemasks are not
663 * supported natively on the target or (like SSE), the target doesn't
664 * have a clean/obvious dotproduct implementation.
665 */
666static void emit_transpose_matrix_transform_vec4( struct tnl_program *p,
667 struct ureg dest,
668 const struct ureg *mat,
669 struct ureg src)
670{
671 struct ureg tmp;
672
673 if (dest.file != PROGRAM_TEMPORARY)
674 tmp = get_temp(p);
675 else
676 tmp = dest;
677
678 emit_op2(p, OPCODE_MUL, tmp, 0, swizzle1(src,X), mat[0]);
679 emit_op3(p, OPCODE_MAD, tmp, 0, swizzle1(src,Y), mat[1], tmp);
680 emit_op3(p, OPCODE_MAD, tmp, 0, swizzle1(src,Z), mat[2], tmp);
681 emit_op3(p, OPCODE_MAD, dest, 0, swizzle1(src,W), mat[3], tmp);
682
683 if (dest.file != PROGRAM_TEMPORARY)
684 release_temp(p, tmp);
685}
686
Brian Paul1d03f282008-12-31 16:38:10 -0700687
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700688static void emit_matrix_transform_vec3( struct tnl_program *p,
689 struct ureg dest,
690 const struct ureg *mat,
691 struct ureg src)
692{
693 emit_op2(p, OPCODE_DP3, dest, WRITEMASK_X, src, mat[0]);
694 emit_op2(p, OPCODE_DP3, dest, WRITEMASK_Y, src, mat[1]);
695 emit_op2(p, OPCODE_DP3, dest, WRITEMASK_Z, src, mat[2]);
696}
697
698
699static void emit_normalize_vec3( struct tnl_program *p,
700 struct ureg dest,
701 struct ureg src )
702{
Briane24afc82008-11-08 10:29:03 -0700703#if 0
704 /* XXX use this when drivers are ready for NRM3 */
705 emit_op1(p, OPCODE_NRM3, dest, WRITEMASK_XYZ, src);
706#else
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700707 struct ureg tmp = get_temp(p);
708 emit_op2(p, OPCODE_DP3, tmp, WRITEMASK_X, src, src);
709 emit_op1(p, OPCODE_RSQ, tmp, WRITEMASK_X, tmp);
710 emit_op2(p, OPCODE_MUL, dest, 0, src, swizzle1(tmp, X));
711 release_temp(p, tmp);
Briane24afc82008-11-08 10:29:03 -0700712#endif
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700713}
714
Brian Paul1d03f282008-12-31 16:38:10 -0700715
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700716static void emit_passthrough( struct tnl_program *p,
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700717 GLuint input,
718 GLuint output )
719{
720 struct ureg out = register_output(p, output);
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700721 emit_op1(p, OPCODE_MOV, out, 0, register_input(p, input));
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700722}
723
Brian Paul1d03f282008-12-31 16:38:10 -0700724
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700725static struct ureg get_eye_position( struct tnl_program *p )
726{
727 if (is_undef(p->eye_position)) {
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700728 struct ureg pos = register_input( p, VERT_ATTRIB_POS );
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700729 struct ureg modelview[4];
730
731 p->eye_position = reserve_temp(p);
732
Keith Whitwellb6e82562009-05-05 12:12:28 +0100733 if (p->mvp_with_dp4) {
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700734 register_matrix_param5( p, STATE_MODELVIEW_MATRIX, 0, 0, 3,
735 0, modelview );
736
737 emit_matrix_transform_vec4(p, p->eye_position, modelview, pos);
738 }
739 else {
740 register_matrix_param5( p, STATE_MODELVIEW_MATRIX, 0, 0, 3,
741 STATE_MATRIX_TRANSPOSE, modelview );
742
743 emit_transpose_matrix_transform_vec4(p, p->eye_position, modelview, pos);
744 }
745 }
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700746
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700747 return p->eye_position;
748}
749
750
751static struct ureg get_eye_position_z( struct tnl_program *p )
752{
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700753 if (!is_undef(p->eye_position))
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700754 return swizzle1(p->eye_position, Z);
755
756 if (is_undef(p->eye_position_z)) {
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700757 struct ureg pos = register_input( p, VERT_ATTRIB_POS );
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700758 struct ureg modelview[4];
759
760 p->eye_position_z = reserve_temp(p);
761
762 register_matrix_param5( p, STATE_MODELVIEW_MATRIX, 0, 0, 3,
763 0, modelview );
764
765 emit_op2(p, OPCODE_DP4, p->eye_position_z, 0, pos, modelview[2]);
766 }
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700767
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700768 return p->eye_position_z;
769}
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700770
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700771
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700772static struct ureg get_eye_position_normalized( struct tnl_program *p )
773{
774 if (is_undef(p->eye_position_normalized)) {
775 struct ureg eye = get_eye_position(p);
776 p->eye_position_normalized = reserve_temp(p);
777 emit_normalize_vec3(p, p->eye_position_normalized, eye);
778 }
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700779
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700780 return p->eye_position_normalized;
781}
782
783
784static struct ureg get_transformed_normal( struct tnl_program *p )
785{
786 if (is_undef(p->transformed_normal) &&
787 !p->state->need_eye_coords &&
788 !p->state->normalize &&
789 !(p->state->need_eye_coords == p->state->rescale_normals))
790 {
791 p->transformed_normal = register_input(p, VERT_ATTRIB_NORMAL );
792 }
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700793 else if (is_undef(p->transformed_normal))
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700794 {
795 struct ureg normal = register_input(p, VERT_ATTRIB_NORMAL );
796 struct ureg mvinv[3];
797 struct ureg transformed_normal = reserve_temp(p);
798
799 if (p->state->need_eye_coords) {
800 register_matrix_param5( p, STATE_MODELVIEW_MATRIX, 0, 0, 2,
801 STATE_MATRIX_INVTRANS, mvinv );
802
803 /* Transform to eye space:
804 */
805 emit_matrix_transform_vec3( p, transformed_normal, mvinv, normal );
806 normal = transformed_normal;
807 }
808
809 /* Normalize/Rescale:
810 */
811 if (p->state->normalize) {
812 emit_normalize_vec3( p, transformed_normal, normal );
813 normal = transformed_normal;
814 }
815 else if (p->state->need_eye_coords == p->state->rescale_normals) {
816 /* This is already adjusted for eye/non-eye rendering:
817 */
818 struct ureg rescale = register_param2(p, STATE_INTERNAL,
819 STATE_NORMAL_SCALE);
820
821 emit_op2( p, OPCODE_MUL, transformed_normal, 0, normal, rescale );
822 normal = transformed_normal;
823 }
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700824
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700825 assert(normal.file == PROGRAM_TEMPORARY);
826 p->transformed_normal = normal;
827 }
828
829 return p->transformed_normal;
830}
831
832
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700833static void build_hpos( struct tnl_program *p )
834{
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700835 struct ureg pos = register_input( p, VERT_ATTRIB_POS );
Paul Berry36b252e2013-02-23 07:22:01 -0800836 struct ureg hpos = register_output( p, VARYING_SLOT_POS );
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700837 struct ureg mvp[4];
838
Keith Whitwellb6e82562009-05-05 12:12:28 +0100839 if (p->mvp_with_dp4) {
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700840 register_matrix_param5( p, STATE_MVP_MATRIX, 0, 0, 3,
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700841 0, mvp );
842 emit_matrix_transform_vec4( p, hpos, mvp, pos );
843 }
844 else {
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700845 register_matrix_param5( p, STATE_MVP_MATRIX, 0, 0, 3,
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700846 STATE_MATRIX_TRANSPOSE, mvp );
847 emit_transpose_matrix_transform_vec4( p, hpos, mvp, pos );
848 }
849}
850
851
852static GLuint material_attrib( GLuint side, GLuint property )
853{
Brian Paul53d50752009-03-31 15:47:05 -0600854 return (property - STATE_AMBIENT) * 2 + side;
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700855}
856
Brian Paul1d03f282008-12-31 16:38:10 -0700857
858/**
859 * Get a bitmask of which material values vary on a per-vertex basis.
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700860 */
861static void set_material_flags( struct tnl_program *p )
862{
863 p->color_materials = 0;
864 p->materials = 0;
865
Keith Whitwellf6c8ca02009-05-31 19:01:55 -0700866 if (p->state->varying_vp_inputs & VERT_BIT_COLOR0) {
867 p->materials =
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700868 p->color_materials = p->state->light_color_material_mask;
869 }
870
Mathias Fröhlichdca6a282011-10-31 18:32:59 +0100871 p->materials |= (p->state->varying_vp_inputs >> VERT_ATTRIB_GENERIC0);
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700872}
873
874
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700875static struct ureg get_material( struct tnl_program *p, GLuint side,
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700876 GLuint property )
877{
878 GLuint attrib = material_attrib(side, property);
879
880 if (p->color_materials & (1<<attrib))
881 return register_input(p, VERT_ATTRIB_COLOR0);
Keith Whitwellf6c8ca02009-05-31 19:01:55 -0700882 else if (p->materials & (1<<attrib)) {
883 /* Put material values in the GENERIC slots -- they are not used
884 * for anything in fixed function mode.
885 */
886 return register_input( p, attrib + VERT_ATTRIB_GENERIC0 );
887 }
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700888 else
889 return register_param3( p, STATE_MATERIAL, side, property );
890}
891
892#define SCENE_COLOR_BITS(side) (( MAT_BIT_FRONT_EMISSION | \
893 MAT_BIT_FRONT_AMBIENT | \
894 MAT_BIT_FRONT_DIFFUSE) << (side))
895
Brian Paul1d03f282008-12-31 16:38:10 -0700896
897/**
898 * Either return a precalculated constant value or emit code to
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700899 * calculate these values dynamically in the case where material calls
900 * are present between begin/end pairs.
901 *
902 * Probably want to shift this to the program compilation phase - if
903 * we always emitted the calculation here, a smart compiler could
904 * detect that it was constant (given a certain set of inputs), and
905 * lift it out of the main loop. That way the programs created here
906 * would be independent of the vertex_buffer details.
907 */
908static struct ureg get_scenecolor( struct tnl_program *p, GLuint side )
909{
910 if (p->materials & SCENE_COLOR_BITS(side)) {
911 struct ureg lm_ambient = register_param1(p, STATE_LIGHTMODEL_AMBIENT);
912 struct ureg material_emission = get_material(p, side, STATE_EMISSION);
913 struct ureg material_ambient = get_material(p, side, STATE_AMBIENT);
914 struct ureg material_diffuse = get_material(p, side, STATE_DIFFUSE);
915 struct ureg tmp = make_temp(p, material_diffuse);
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700916 emit_op3(p, OPCODE_MAD, tmp, WRITEMASK_XYZ, lm_ambient,
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700917 material_ambient, material_emission);
918 return tmp;
919 }
920 else
921 return register_param2( p, STATE_LIGHTMODEL_SCENECOLOR, side );
922}
923
924
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700925static struct ureg get_lightprod( struct tnl_program *p, GLuint light,
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700926 GLuint side, GLuint property )
927{
928 GLuint attrib = material_attrib(side, property);
929 if (p->materials & (1<<attrib)) {
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700930 struct ureg light_value =
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700931 register_param3(p, STATE_LIGHT, light, property);
932 struct ureg material_value = get_material(p, side, property);
933 struct ureg tmp = get_temp(p);
Brian Paul53d50752009-03-31 15:47:05 -0600934 emit_op2(p, OPCODE_MUL, tmp, 0, light_value, material_value);
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700935 return tmp;
936 }
937 else
938 return register_param4(p, STATE_LIGHTPROD, light, side, property);
939}
940
Brian Paul1d03f282008-12-31 16:38:10 -0700941
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700942static struct ureg calculate_light_attenuation( struct tnl_program *p,
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700943 GLuint i,
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700944 struct ureg VPpli,
945 struct ureg dist )
946{
947 struct ureg attenuation = register_param3(p, STATE_LIGHT, i,
948 STATE_ATTENUATION);
Yuanhan Liu4f677ca2011-11-16 11:29:08 +0800949 struct ureg att = undef;
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700950
951 /* Calculate spot attenuation:
952 */
953 if (!p->state->unit[i].light_spotcutoff_is_180) {
954 struct ureg spot_dir_norm = register_param3(p, STATE_INTERNAL,
955 STATE_LIGHT_SPOT_DIR_NORMALIZED, i);
956 struct ureg spot = get_temp(p);
957 struct ureg slt = get_temp(p);
958
Yuanhan Liu4f677ca2011-11-16 11:29:08 +0800959 att = get_temp(p);
960
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700961 emit_op2(p, OPCODE_DP3, spot, 0, negate(VPpli), spot_dir_norm);
962 emit_op2(p, OPCODE_SLT, slt, 0, swizzle1(spot_dir_norm,W), spot);
963 emit_op2(p, OPCODE_POW, spot, 0, spot, swizzle1(attenuation, W));
964 emit_op2(p, OPCODE_MUL, att, 0, slt, spot);
965
966 release_temp(p, spot);
967 release_temp(p, slt);
968 }
969
Yuanhan Liu4f677ca2011-11-16 11:29:08 +0800970 /* Calculate distance attenuation(See formula (2.4) at glspec 2.1 page 62):
971 *
972 * Skip the calucation when _dist_ is undefined(light_eyepos3_is_zero)
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700973 */
Yuanhan Liu4f677ca2011-11-16 11:29:08 +0800974 if (p->state->unit[i].light_attenuated && !is_undef(dist)) {
975 if (is_undef(att))
976 att = get_temp(p);
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700977 /* 1/d,d,d,1/d */
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700978 emit_op1(p, OPCODE_RCP, dist, WRITEMASK_YZ, dist);
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700979 /* 1,d,d*d,1/d */
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700980 emit_op2(p, OPCODE_MUL, dist, WRITEMASK_XZ, dist, swizzle1(dist,Y));
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700981 /* 1/dist-atten */
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700982 emit_op2(p, OPCODE_DP3, dist, 0, attenuation, dist);
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700983
984 if (!p->state->unit[i].light_spotcutoff_is_180) {
985 /* dist-atten */
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700986 emit_op1(p, OPCODE_RCP, dist, 0, dist);
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700987 /* spot-atten * dist-atten */
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700988 emit_op2(p, OPCODE_MUL, att, 0, dist, att);
Brian Paul53d50752009-03-31 15:47:05 -0600989 }
990 else {
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700991 /* dist-atten */
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700992 emit_op1(p, OPCODE_RCP, att, 0, dist);
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700993 }
994 }
995
996 return att;
997}
Keith Whitwell6dfb89e2009-05-31 19:04:06 -0700998
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700999
1000/**
1001 * Compute:
1002 * lit.y = MAX(0, dots.x)
1003 * lit.z = SLT(0, dots.x)
1004 */
1005static void emit_degenerate_lit( struct tnl_program *p,
1006 struct ureg lit,
1007 struct ureg dots )
1008{
1009 struct ureg id = get_identity_param(p); /* id = {0,0,0,1} */
1010
1011 /* Note that lit.x & lit.w will not be examined. Note also that
1012 * dots.xyzw == dots.xxxx.
1013 */
1014
1015 /* MAX lit, id, dots;
1016 */
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001017 emit_op2(p, OPCODE_MAX, lit, WRITEMASK_XYZW, id, dots);
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001018
1019 /* result[2] = (in > 0 ? 1 : 0)
1020 * SLT lit.z, id.z, dots; # lit.z = (0 < dots.z) ? 1 : 0
1021 */
1022 emit_op2(p, OPCODE_SLT, lit, WRITEMASK_Z, swizzle1(id,Z), dots);
1023}
1024
1025
1026/* Need to add some addtional parameters to allow lighting in object
1027 * space - STATE_SPOT_DIRECTION and STATE_HALF_VECTOR implicitly assume eye
1028 * space lighting.
1029 */
1030static void build_lighting( struct tnl_program *p )
1031{
1032 const GLboolean twoside = p->state->light_twoside;
1033 const GLboolean separate = p->state->separate_specular;
1034 GLuint nr_lights = 0, count = 0;
1035 struct ureg normal = get_transformed_normal(p);
1036 struct ureg lit = get_temp(p);
1037 struct ureg dots = get_temp(p);
1038 struct ureg _col0 = undef, _col1 = undef;
1039 struct ureg _bfc0 = undef, _bfc1 = undef;
1040 GLuint i;
1041
1042 /*
1043 * NOTE:
Brian Paul53d50752009-03-31 15:47:05 -06001044 * dots.x = dot(normal, VPpli)
1045 * dots.y = dot(normal, halfAngle)
1046 * dots.z = back.shininess
1047 * dots.w = front.shininess
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001048 */
1049
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001050 for (i = 0; i < MAX_LIGHTS; i++)
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001051 if (p->state->unit[i].light_enabled)
1052 nr_lights++;
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001053
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001054 set_material_flags(p);
1055
1056 {
1057 if (!p->state->material_shininess_is_zero) {
1058 struct ureg shininess = get_material(p, 0, STATE_SHININESS);
Brian Paul53d50752009-03-31 15:47:05 -06001059 emit_op1(p, OPCODE_MOV, dots, WRITEMASK_W, swizzle1(shininess,X));
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001060 release_temp(p, shininess);
1061 }
1062
1063 _col0 = make_temp(p, get_scenecolor(p, 0));
1064 if (separate)
1065 _col1 = make_temp(p, get_identity_param(p));
1066 else
1067 _col1 = _col0;
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001068 }
1069
1070 if (twoside) {
1071 if (!p->state->material_shininess_is_zero) {
Brian Paul919f5702009-03-31 16:13:14 -06001072 /* Note that we negate the back-face specular exponent here.
1073 * The negation will be un-done later in the back-face code below.
1074 */
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001075 struct ureg shininess = get_material(p, 1, STATE_SHININESS);
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001076 emit_op1(p, OPCODE_MOV, dots, WRITEMASK_Z,
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001077 negate(swizzle1(shininess,X)));
1078 release_temp(p, shininess);
1079 }
1080
1081 _bfc0 = make_temp(p, get_scenecolor(p, 1));
1082 if (separate)
1083 _bfc1 = make_temp(p, get_identity_param(p));
1084 else
1085 _bfc1 = _bfc0;
1086 }
1087
1088 /* If no lights, still need to emit the scenecolor.
1089 */
1090 {
Paul Berry36b252e2013-02-23 07:22:01 -08001091 struct ureg res0 = register_output( p, VARYING_SLOT_COL0 );
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001092 emit_op1(p, OPCODE_MOV, res0, 0, _col0);
1093 }
1094
1095 if (separate) {
Paul Berry36b252e2013-02-23 07:22:01 -08001096 struct ureg res1 = register_output( p, VARYING_SLOT_COL1 );
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001097 emit_op1(p, OPCODE_MOV, res1, 0, _col1);
1098 }
1099
1100 if (twoside) {
Paul Berry36b252e2013-02-23 07:22:01 -08001101 struct ureg res0 = register_output( p, VARYING_SLOT_BFC0 );
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001102 emit_op1(p, OPCODE_MOV, res0, 0, _bfc0);
1103 }
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001104
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001105 if (twoside && separate) {
Paul Berry36b252e2013-02-23 07:22:01 -08001106 struct ureg res1 = register_output( p, VARYING_SLOT_BFC1 );
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001107 emit_op1(p, OPCODE_MOV, res1, 0, _bfc1);
1108 }
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001109
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001110 if (nr_lights == 0) {
1111 release_temps(p);
1112 return;
1113 }
1114
1115 for (i = 0; i < MAX_LIGHTS; i++) {
1116 if (p->state->unit[i].light_enabled) {
1117 struct ureg half = undef;
1118 struct ureg att = undef, VPpli = undef;
Yuanhan Liu4f677ca2011-11-16 11:29:08 +08001119 struct ureg dist = undef;
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001120
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001121 count++;
Yuanhan Liu4f677ca2011-11-16 11:29:08 +08001122 if (p->state->unit[i].light_eyepos3_is_zero) {
1123 VPpli = register_param3(p, STATE_INTERNAL,
1124 STATE_LIGHT_POSITION_NORMALIZED, i);
1125 } else {
1126 struct ureg Ppli = register_param3(p, STATE_INTERNAL,
1127 STATE_LIGHT_POSITION, i);
1128 struct ureg V = get_eye_position(p);
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001129
Yuanhan Liu4f677ca2011-11-16 11:29:08 +08001130 VPpli = get_temp(p);
1131 dist = get_temp(p);
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001132
Yuanhan Liu4f677ca2011-11-16 11:29:08 +08001133 /* Calculate VPpli vector
1134 */
1135 emit_op2(p, OPCODE_SUB, VPpli, 0, Ppli, V);
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001136
Yuanhan Liu4f677ca2011-11-16 11:29:08 +08001137 /* Normalize VPpli. The dist value also used in
1138 * attenuation below.
1139 */
1140 emit_op2(p, OPCODE_DP3, dist, 0, VPpli, VPpli);
1141 emit_op1(p, OPCODE_RSQ, dist, 0, dist);
1142 emit_op2(p, OPCODE_MUL, VPpli, 0, VPpli, dist);
1143 }
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001144
Yuanhan Liu4f677ca2011-11-16 11:29:08 +08001145 /* Calculate attenuation:
1146 */
1147 att = calculate_light_attenuation(p, i, VPpli, dist);
1148 release_temp(p, dist);
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001149
Yuanhan Liu4f677ca2011-11-16 11:29:08 +08001150 /* Calculate viewer direction, or use infinite viewer:
1151 */
1152 if (!p->state->material_shininess_is_zero) {
1153 if (p->state->light_local_viewer) {
1154 struct ureg eye_hat = get_eye_position_normalized(p);
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001155 half = get_temp(p);
Yuanhan Liu4f677ca2011-11-16 11:29:08 +08001156 emit_op2(p, OPCODE_SUB, half, 0, VPpli, eye_hat);
1157 emit_normalize_vec3(p, half, half);
1158 } else if (p->state->unit[i].light_eyepos3_is_zero) {
1159 half = register_param3(p, STATE_INTERNAL,
1160 STATE_LIGHT_HALF_VECTOR, i);
1161 } else {
1162 struct ureg z_dir = swizzle(get_identity_param(p),X,Y,W,Z);
1163 half = get_temp(p);
1164 emit_op2(p, OPCODE_ADD, half, 0, VPpli, z_dir);
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001165 emit_normalize_vec3(p, half, half);
1166 }
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001167 }
1168
1169 /* Calculate dot products:
1170 */
1171 if (p->state->material_shininess_is_zero) {
1172 emit_op2(p, OPCODE_DP3, dots, 0, normal, VPpli);
1173 }
1174 else {
1175 emit_op2(p, OPCODE_DP3, dots, WRITEMASK_X, normal, VPpli);
1176 emit_op2(p, OPCODE_DP3, dots, WRITEMASK_Y, normal, half);
1177 }
1178
1179 /* Front face lighting:
1180 */
1181 {
1182 struct ureg ambient = get_lightprod(p, i, 0, STATE_AMBIENT);
1183 struct ureg diffuse = get_lightprod(p, i, 0, STATE_DIFFUSE);
1184 struct ureg specular = get_lightprod(p, i, 0, STATE_SPECULAR);
1185 struct ureg res0, res1;
1186 GLuint mask0, mask1;
1187
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001188 if (count == nr_lights) {
1189 if (separate) {
1190 mask0 = WRITEMASK_XYZ;
1191 mask1 = WRITEMASK_XYZ;
Paul Berry36b252e2013-02-23 07:22:01 -08001192 res0 = register_output( p, VARYING_SLOT_COL0 );
1193 res1 = register_output( p, VARYING_SLOT_COL1 );
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001194 }
1195 else {
1196 mask0 = 0;
1197 mask1 = WRITEMASK_XYZ;
1198 res0 = _col0;
Paul Berry36b252e2013-02-23 07:22:01 -08001199 res1 = register_output( p, VARYING_SLOT_COL0 );
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001200 }
Brian Paul53d50752009-03-31 15:47:05 -06001201 }
1202 else {
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001203 mask0 = 0;
1204 mask1 = 0;
1205 res0 = _col0;
1206 res1 = _col1;
1207 }
1208
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001209 if (!is_undef(att)) {
1210 /* light is attenuated by distance */
1211 emit_op1(p, OPCODE_LIT, lit, 0, dots);
1212 emit_op2(p, OPCODE_MUL, lit, 0, lit, att);
1213 emit_op3(p, OPCODE_MAD, _col0, 0, swizzle1(lit,X), ambient, _col0);
Brian Paul53d50752009-03-31 15:47:05 -06001214 }
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001215 else if (!p->state->material_shininess_is_zero) {
1216 /* there's a non-zero specular term */
1217 emit_op1(p, OPCODE_LIT, lit, 0, dots);
1218 emit_op2(p, OPCODE_ADD, _col0, 0, ambient, _col0);
Brian Paul53d50752009-03-31 15:47:05 -06001219 }
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001220 else {
1221 /* no attenutation, no specular */
1222 emit_degenerate_lit(p, lit, dots);
1223 emit_op2(p, OPCODE_ADD, _col0, 0, ambient, _col0);
1224 }
1225
1226 emit_op3(p, OPCODE_MAD, res0, mask0, swizzle1(lit,Y), diffuse, _col0);
1227 emit_op3(p, OPCODE_MAD, res1, mask1, swizzle1(lit,Z), specular, _col1);
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001228
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001229 release_temp(p, ambient);
1230 release_temp(p, diffuse);
1231 release_temp(p, specular);
1232 }
1233
1234 /* Back face lighting:
1235 */
1236 if (twoside) {
1237 struct ureg ambient = get_lightprod(p, i, 1, STATE_AMBIENT);
1238 struct ureg diffuse = get_lightprod(p, i, 1, STATE_DIFFUSE);
1239 struct ureg specular = get_lightprod(p, i, 1, STATE_SPECULAR);
1240 struct ureg res0, res1;
1241 GLuint mask0, mask1;
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001242
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001243 if (count == nr_lights) {
1244 if (separate) {
1245 mask0 = WRITEMASK_XYZ;
1246 mask1 = WRITEMASK_XYZ;
Paul Berry36b252e2013-02-23 07:22:01 -08001247 res0 = register_output( p, VARYING_SLOT_BFC0 );
1248 res1 = register_output( p, VARYING_SLOT_BFC1 );
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001249 }
1250 else {
1251 mask0 = 0;
1252 mask1 = WRITEMASK_XYZ;
1253 res0 = _bfc0;
Paul Berry36b252e2013-02-23 07:22:01 -08001254 res1 = register_output( p, VARYING_SLOT_BFC0 );
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001255 }
Brian Paul53d50752009-03-31 15:47:05 -06001256 }
1257 else {
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001258 res0 = _bfc0;
1259 res1 = _bfc1;
1260 mask0 = 0;
1261 mask1 = 0;
1262 }
1263
Brian Paul919f5702009-03-31 16:13:14 -06001264 /* For the back face we need to negate the X and Y component
1265 * dot products. dots.Z has the negated back-face specular
1266 * exponent. We swizzle that into the W position. This
1267 * negation makes the back-face specular term positive again.
1268 */
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001269 dots = negate(swizzle(dots,X,Y,W,Z));
1270
1271 if (!is_undef(att)) {
1272 emit_op1(p, OPCODE_LIT, lit, 0, dots);
1273 emit_op2(p, OPCODE_MUL, lit, 0, lit, att);
1274 emit_op3(p, OPCODE_MAD, _bfc0, 0, swizzle1(lit,X), ambient, _bfc0);
1275 }
1276 else if (!p->state->material_shininess_is_zero) {
1277 emit_op1(p, OPCODE_LIT, lit, 0, dots);
Brian Paul53d50752009-03-31 15:47:05 -06001278 emit_op2(p, OPCODE_ADD, _bfc0, 0, ambient, _bfc0); /**/
1279 }
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001280 else {
1281 emit_degenerate_lit(p, lit, dots);
Xiang, Haihao4550b052008-11-07 14:58:04 +08001282 emit_op2(p, OPCODE_ADD, _bfc0, 0, ambient, _bfc0);
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001283 }
1284
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001285 emit_op3(p, OPCODE_MAD, res0, mask0, swizzle1(lit,Y), diffuse, _bfc0);
1286 emit_op3(p, OPCODE_MAD, res1, mask1, swizzle1(lit,Z), specular, _bfc1);
Brian Paul919f5702009-03-31 16:13:14 -06001287 /* restore dots to its original state for subsequent lights
1288 * by negating and swizzling again.
1289 */
1290 dots = negate(swizzle(dots,X,Y,W,Z));
Xiang, Haihao8412b062008-11-11 13:36:32 +08001291
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001292 release_temp(p, ambient);
1293 release_temp(p, diffuse);
1294 release_temp(p, specular);
1295 }
1296
1297 release_temp(p, half);
1298 release_temp(p, VPpli);
1299 release_temp(p, att);
1300 }
1301 }
1302
1303 release_temps( p );
1304}
1305
1306
1307static void build_fog( struct tnl_program *p )
1308{
Paul Berry36b252e2013-02-23 07:22:01 -08001309 struct ureg fog = register_output(p, VARYING_SLOT_FOGC);
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001310 struct ureg input;
1311
1312 if (p->state->fog_source_is_depth) {
Nicholas Miellfadd5e12011-09-19 23:17:00 -07001313
1314 switch (p->state->fog_distance_mode) {
1315 case FDM_EYE_RADIAL: /* Z = sqrt(Xe*Xe + Ye*Ye + Ze*Ze) */
1316 input = get_eye_position(p);
1317 emit_op2(p, OPCODE_DP3, fog, WRITEMASK_X, input, input);
1318 emit_op1(p, OPCODE_RSQ, fog, WRITEMASK_X, fog);
1319 emit_op1(p, OPCODE_RCP, fog, WRITEMASK_X, fog);
1320 break;
1321 case FDM_EYE_PLANE: /* Z = Ze */
1322 input = get_eye_position_z(p);
1323 emit_op1(p, OPCODE_MOV, fog, WRITEMASK_X, input);
1324 break;
1325 case FDM_EYE_PLANE_ABS: /* Z = abs(Ze) */
1326 input = get_eye_position_z(p);
1327 emit_op1(p, OPCODE_ABS, fog, WRITEMASK_X, input);
1328 break;
1329 default: assert(0); break; /* can't happen */
1330 }
1331
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001332 }
1333 else {
1334 input = swizzle1(register_input(p, VERT_ATTRIB_FOG), X);
Nicholas Miellfadd5e12011-09-19 23:17:00 -07001335 emit_op1(p, OPCODE_ABS, fog, WRITEMASK_X, input);
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001336 }
1337
Brian Paulee0984e2009-08-18 17:39:55 -06001338 emit_op1(p, OPCODE_MOV, fog, WRITEMASK_YZW, get_identity_param(p));
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001339}
Brian Paul1d03f282008-12-31 16:38:10 -07001340
Keith Whitwell79047cc2009-05-31 19:03:02 -07001341
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001342static void build_reflect_texgen( struct tnl_program *p,
1343 struct ureg dest,
1344 GLuint writemask )
1345{
1346 struct ureg normal = get_transformed_normal(p);
1347 struct ureg eye_hat = get_eye_position_normalized(p);
1348 struct ureg tmp = get_temp(p);
1349
1350 /* n.u */
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001351 emit_op2(p, OPCODE_DP3, tmp, 0, normal, eye_hat);
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001352 /* 2n.u */
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001353 emit_op2(p, OPCODE_ADD, tmp, 0, tmp, tmp);
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001354 /* (-2n.u)n + u */
1355 emit_op3(p, OPCODE_MAD, dest, writemask, negate(tmp), normal, eye_hat);
1356
1357 release_temp(p, tmp);
1358}
1359
Brian Paul1d03f282008-12-31 16:38:10 -07001360
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001361static void build_sphere_texgen( struct tnl_program *p,
1362 struct ureg dest,
1363 GLuint writemask )
1364{
1365 struct ureg normal = get_transformed_normal(p);
1366 struct ureg eye_hat = get_eye_position_normalized(p);
1367 struct ureg tmp = get_temp(p);
1368 struct ureg half = register_scalar_const(p, .5);
1369 struct ureg r = get_temp(p);
1370 struct ureg inv_m = get_temp(p);
1371 struct ureg id = get_identity_param(p);
1372
1373 /* Could share the above calculations, but it would be
1374 * a fairly odd state for someone to set (both sphere and
1375 * reflection active for different texture coordinate
1376 * components. Of course - if two texture units enable
1377 * reflect and/or sphere, things start to tilt in favour
1378 * of seperating this out:
1379 */
1380
1381 /* n.u */
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001382 emit_op2(p, OPCODE_DP3, tmp, 0, normal, eye_hat);
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001383 /* 2n.u */
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001384 emit_op2(p, OPCODE_ADD, tmp, 0, tmp, tmp);
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001385 /* (-2n.u)n + u */
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001386 emit_op3(p, OPCODE_MAD, r, 0, negate(tmp), normal, eye_hat);
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001387 /* r + 0,0,1 */
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001388 emit_op2(p, OPCODE_ADD, tmp, 0, r, swizzle(id,X,Y,W,Z));
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001389 /* rx^2 + ry^2 + (rz+1)^2 */
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001390 emit_op2(p, OPCODE_DP3, tmp, 0, tmp, tmp);
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001391 /* 2/m */
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001392 emit_op1(p, OPCODE_RSQ, tmp, 0, tmp);
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001393 /* 1/m */
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001394 emit_op2(p, OPCODE_MUL, inv_m, 0, tmp, half);
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001395 /* r/m + 1/2 */
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001396 emit_op3(p, OPCODE_MAD, dest, writemask, r, inv_m, half);
1397
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001398 release_temp(p, tmp);
1399 release_temp(p, r);
1400 release_temp(p, inv_m);
1401}
1402
1403
1404static void build_texture_transform( struct tnl_program *p )
1405{
1406 GLuint i, j;
1407
Brian Paule9b34882008-12-31 11:54:02 -07001408 for (i = 0; i < MAX_TEXTURE_COORD_UNITS; i++) {
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001409
Paul Berryeed6baf2013-02-23 09:00:58 -08001410 if (!(p->state->fragprog_inputs_read & VARYING_BIT_TEX(i)))
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001411 continue;
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001412
Dave Airlie911fa4a2010-04-12 15:40:29 +10001413 if (p->state->unit[i].coord_replace)
1414 continue;
1415
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001416 if (p->state->unit[i].texgen_enabled ||
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001417 p->state->unit[i].texmat_enabled) {
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001418
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001419 GLuint texmat_enabled = p->state->unit[i].texmat_enabled;
Paul Berry36b252e2013-02-23 07:22:01 -08001420 struct ureg out = register_output(p, VARYING_SLOT_TEX0 + i);
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001421 struct ureg out_texgen = undef;
1422
1423 if (p->state->unit[i].texgen_enabled) {
1424 GLuint copy_mask = 0;
1425 GLuint sphere_mask = 0;
1426 GLuint reflect_mask = 0;
1427 GLuint normal_mask = 0;
1428 GLuint modes[4];
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001429
1430 if (texmat_enabled)
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001431 out_texgen = get_temp(p);
1432 else
1433 out_texgen = out;
1434
1435 modes[0] = p->state->unit[i].texgen_mode0;
1436 modes[1] = p->state->unit[i].texgen_mode1;
1437 modes[2] = p->state->unit[i].texgen_mode2;
1438 modes[3] = p->state->unit[i].texgen_mode3;
1439
1440 for (j = 0; j < 4; j++) {
1441 switch (modes[j]) {
1442 case TXG_OBJ_LINEAR: {
1443 struct ureg obj = register_input(p, VERT_ATTRIB_POS);
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001444 struct ureg plane =
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001445 register_param3(p, STATE_TEXGEN, i,
1446 STATE_TEXGEN_OBJECT_S + j);
1447
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001448 emit_op2(p, OPCODE_DP4, out_texgen, WRITEMASK_X << j,
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001449 obj, plane );
1450 break;
1451 }
1452 case TXG_EYE_LINEAR: {
1453 struct ureg eye = get_eye_position(p);
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001454 struct ureg plane =
1455 register_param3(p, STATE_TEXGEN, i,
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001456 STATE_TEXGEN_EYE_S + j);
1457
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001458 emit_op2(p, OPCODE_DP4, out_texgen, WRITEMASK_X << j,
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001459 eye, plane );
1460 break;
1461 }
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001462 case TXG_SPHERE_MAP:
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001463 sphere_mask |= WRITEMASK_X << j;
1464 break;
1465 case TXG_REFLECTION_MAP:
1466 reflect_mask |= WRITEMASK_X << j;
1467 break;
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001468 case TXG_NORMAL_MAP:
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001469 normal_mask |= WRITEMASK_X << j;
1470 break;
1471 case TXG_NONE:
1472 copy_mask |= WRITEMASK_X << j;
1473 }
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001474 }
1475
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001476 if (sphere_mask) {
1477 build_sphere_texgen(p, out_texgen, sphere_mask);
1478 }
1479
1480 if (reflect_mask) {
1481 build_reflect_texgen(p, out_texgen, reflect_mask);
1482 }
1483
1484 if (normal_mask) {
1485 struct ureg normal = get_transformed_normal(p);
1486 emit_op1(p, OPCODE_MOV, out_texgen, normal_mask, normal );
1487 }
1488
1489 if (copy_mask) {
1490 struct ureg in = register_input(p, VERT_ATTRIB_TEX0+i);
1491 emit_op1(p, OPCODE_MOV, out_texgen, copy_mask, in );
1492 }
1493 }
1494
1495 if (texmat_enabled) {
1496 struct ureg texmat[4];
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001497 struct ureg in = (!is_undef(out_texgen) ?
1498 out_texgen :
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001499 register_input(p, VERT_ATTRIB_TEX0+i));
Keith Whitwellb6e82562009-05-05 12:12:28 +01001500 if (p->mvp_with_dp4) {
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001501 register_matrix_param5( p, STATE_TEXTURE_MATRIX, i, 0, 3,
1502 0, texmat );
1503 emit_matrix_transform_vec4( p, out, texmat, in );
1504 }
1505 else {
1506 register_matrix_param5( p, STATE_TEXTURE_MATRIX, i, 0, 3,
1507 STATE_MATRIX_TRANSPOSE, texmat );
1508 emit_transpose_matrix_transform_vec4( p, out, texmat, in );
1509 }
1510 }
1511
1512 release_temps(p);
Brian Paul53d50752009-03-31 15:47:05 -06001513 }
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001514 else {
Paul Berry36b252e2013-02-23 07:22:01 -08001515 emit_passthrough(p, VERT_ATTRIB_TEX0+i, VARYING_SLOT_TEX0+i);
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001516 }
1517 }
1518}
1519
1520
1521/**
1522 * Point size attenuation computation.
1523 */
1524static void build_atten_pointsize( struct tnl_program *p )
1525{
1526 struct ureg eye = get_eye_position_z(p);
Roland Scheidegger2c326e72010-02-04 19:23:09 +01001527 struct ureg state_size = register_param2(p, STATE_INTERNAL, STATE_POINT_SIZE_CLAMPED);
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001528 struct ureg state_attenuation = register_param1(p, STATE_POINT_ATTENUATION);
Paul Berry36b252e2013-02-23 07:22:01 -08001529 struct ureg out = register_output(p, VARYING_SLOT_PSIZ);
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001530 struct ureg ut = get_temp(p);
1531
1532 /* dist = |eyez| */
1533 emit_op1(p, OPCODE_ABS, ut, WRITEMASK_Y, swizzle1(eye, Z));
1534 /* p1 + dist * (p2 + dist * p3); */
1535 emit_op3(p, OPCODE_MAD, ut, WRITEMASK_X, swizzle1(ut, Y),
1536 swizzle1(state_attenuation, Z), swizzle1(state_attenuation, Y));
1537 emit_op3(p, OPCODE_MAD, ut, WRITEMASK_X, swizzle1(ut, Y),
1538 ut, swizzle1(state_attenuation, X));
1539
1540 /* 1 / sqrt(factor) */
1541 emit_op1(p, OPCODE_RSQ, ut, WRITEMASK_X, ut );
1542
1543#if 0
1544 /* out = pointSize / sqrt(factor) */
1545 emit_op2(p, OPCODE_MUL, out, WRITEMASK_X, ut, state_size);
1546#else
1547 /* this is a good place to clamp the point size since there's likely
1548 * no hardware registers to clamp point size at rasterization time.
1549 */
1550 emit_op2(p, OPCODE_MUL, ut, WRITEMASK_X, ut, state_size);
1551 emit_op2(p, OPCODE_MAX, ut, WRITEMASK_X, ut, swizzle1(state_size, Y));
1552 emit_op2(p, OPCODE_MIN, out, WRITEMASK_X, ut, swizzle1(state_size, Z));
1553#endif
1554
1555 release_temp(p, ut);
1556}
1557
Brian Paul1d03f282008-12-31 16:38:10 -07001558
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001559/**
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001560 * Pass-though per-vertex point size, from user's point size array.
1561 */
1562static void build_array_pointsize( struct tnl_program *p )
1563{
1564 struct ureg in = register_input(p, VERT_ATTRIB_POINT_SIZE);
Paul Berry36b252e2013-02-23 07:22:01 -08001565 struct ureg out = register_output(p, VARYING_SLOT_PSIZ);
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001566 emit_op1(p, OPCODE_MOV, out, WRITEMASK_X, in);
1567}
1568
1569
1570static void build_tnl_program( struct tnl_program *p )
Brian Paul53d50752009-03-31 15:47:05 -06001571{
Brian Paula271a0c2012-07-24 09:54:50 -06001572 /* Emit the program, starting with the modelview, projection transforms:
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001573 */
1574 build_hpos(p);
1575
1576 /* Lighting calculations:
1577 */
Paul Berryeed6baf2013-02-23 09:00:58 -08001578 if (p->state->fragprog_inputs_read & (VARYING_BIT_COL0|VARYING_BIT_COL1)) {
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001579 if (p->state->light_global_enabled)
1580 build_lighting(p);
1581 else {
Paul Berryeed6baf2013-02-23 09:00:58 -08001582 if (p->state->fragprog_inputs_read & VARYING_BIT_COL0)
Paul Berry36b252e2013-02-23 07:22:01 -08001583 emit_passthrough(p, VERT_ATTRIB_COLOR0, VARYING_SLOT_COL0);
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001584
Paul Berryeed6baf2013-02-23 09:00:58 -08001585 if (p->state->fragprog_inputs_read & VARYING_BIT_COL1)
Paul Berry36b252e2013-02-23 07:22:01 -08001586 emit_passthrough(p, VERT_ATTRIB_COLOR1, VARYING_SLOT_COL1);
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001587 }
1588 }
1589
Paul Berryeed6baf2013-02-23 09:00:58 -08001590 if (p->state->fragprog_inputs_read & VARYING_BIT_FOGC)
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001591 build_fog(p);
1592
Paul Berryeed6baf2013-02-23 09:00:58 -08001593 if (p->state->fragprog_inputs_read & VARYING_BITS_TEX_ANY)
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001594 build_texture_transform(p);
1595
1596 if (p->state->point_attenuated)
1597 build_atten_pointsize(p);
1598 else if (p->state->point_array)
1599 build_array_pointsize(p);
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001600
1601 /* Finish up:
1602 */
1603 emit_op1(p, OPCODE_END, undef, 0, undef);
1604
1605 /* Disassemble:
1606 */
1607 if (DISASSEM) {
Kristian Høgsberg298be2b2010-02-19 12:32:24 -05001608 printf ("\n");
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001609 }
1610}
1611
1612
1613static void
1614create_new_program( const struct state_key *key,
1615 struct gl_vertex_program *program,
Keith Whitwellb6e82562009-05-05 12:12:28 +01001616 GLboolean mvp_with_dp4,
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001617 GLuint max_temps)
1618{
1619 struct tnl_program p;
1620
Kenneth Graunke26f8fad2010-02-18 23:51:00 -08001621 memset(&p, 0, sizeof(p));
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001622 p.state = key;
1623 p.program = program;
1624 p.eye_position = undef;
1625 p.eye_position_z = undef;
1626 p.eye_position_normalized = undef;
1627 p.transformed_normal = undef;
1628 p.identity = undef;
1629 p.temp_in_use = 0;
Keith Whitwellb6e82562009-05-05 12:12:28 +01001630 p.mvp_with_dp4 = mvp_with_dp4;
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001631
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001632 if (max_temps >= sizeof(int) * 8)
1633 p.temp_reserved = 0;
1634 else
1635 p.temp_reserved = ~((1<<max_temps)-1);
1636
1637 /* Start by allocating 32 instructions.
1638 * If we need more, we'll grow the instruction array as needed.
1639 */
1640 p.max_inst = 32;
1641 p.program->Base.Instructions = _mesa_alloc_instructions(p.max_inst);
1642 p.program->Base.String = NULL;
1643 p.program->Base.NumInstructions =
1644 p.program->Base.NumTemporaries =
1645 p.program->Base.NumParameters =
1646 p.program->Base.NumAttributes = p.program->Base.NumAddressRegs = 0;
1647 p.program->Base.Parameters = _mesa_new_parameter_list();
1648 p.program->Base.InputsRead = 0;
1649 p.program->Base.OutputsWritten = 0;
1650
1651 build_tnl_program( &p );
1652}
1653
1654
1655/**
1656 * Return a vertex program which implements the current fixed-function
1657 * transform/lighting/texgen operations.
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001658 */
1659struct gl_vertex_program *
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04001660_mesa_get_fixed_func_vertex_program(struct gl_context *ctx)
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001661{
1662 struct gl_vertex_program *prog;
Shunichi Fujie095d582008-09-26 18:55:13 +09001663 struct state_key key;
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001664
1665 /* Grab all the relevent state and put it in a single structure:
1666 */
Shunichi Fujie095d582008-09-26 18:55:13 +09001667 make_state_key(ctx, &key);
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001668
1669 /* Look for an already-prepared program for this state:
1670 */
Brian Paule4f8d332012-07-18 15:33:11 -06001671 prog = gl_vertex_program(
1672 _mesa_search_program_cache(ctx->VertexProgram.Cache, &key, sizeof(key)));
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001673
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001674 if (!prog) {
1675 /* OK, we'll have to build a new one */
1676 if (0)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -05001677 printf("Build new TNL program\n");
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001678
Brian Paule4f8d332012-07-18 15:33:11 -06001679 prog = gl_vertex_program(ctx->Driver.NewProgram(ctx, GL_VERTEX_PROGRAM_ARB, 0));
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001680 if (!prog)
1681 return NULL;
1682
Shunichi Fujie095d582008-09-26 18:55:13 +09001683 create_new_program( &key, prog,
Keith Whitwellb6e82562009-05-05 12:12:28 +01001684 ctx->mvp_with_dp4,
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001685 ctx->Const.VertexProgram.MaxTemps );
1686
1687#if 0
1688 if (ctx->Driver.ProgramStringNotify)
Keith Whitwell6dfb89e2009-05-31 19:04:06 -07001689 ctx->Driver.ProgramStringNotify( ctx, GL_VERTEX_PROGRAM_ARB,
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001690 &prog->Base );
1691#endif
1692 _mesa_program_cache_insert(ctx, ctx->VertexProgram.Cache,
Shunichi Fujie095d582008-09-26 18:55:13 +09001693 &key, sizeof(key), &prog->Base);
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001694 }
1695
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001696 return prog;
1697}