VertexProgram.cpp revision 25d47fc9eb8e8aaf864fab0d6aa7305d034d807b
1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// SwiftShader Software Renderer
2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
3d4ae863d01d5f448dbbba6be4ecc161971a2324fJohn Bauman// Copyright(c) 2005-2013 TransGaming Inc.
4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// All rights reserved. No part of this software may be copied, distributed, transmitted,
6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// transcribed, stored in a retrieval system, translated into any human or computer
7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// language by any means, or disclosed to third parties without the explicit written
8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express
9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// or implied, including but not limited to any patent rights, are granted to you.
10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "VertexProgram.hpp"
13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "Renderer.hpp"
15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "VertexShader.hpp"
16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "Vertex.hpp"
17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "Half.hpp"
18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "SamplerCore.hpp"
19894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "Debug.hpp"
20894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace sw
22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman{
2319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	VertexProgram::VertexProgram(const VertexProcessor::State &state, const VertexShader *shader) : VertexRoutine(state, shader)
24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	{
25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		ifDepth = 0;
26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		loopRepDepth = 0;
27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		breakDepth = 0;
2819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		currentLabel = -1;
2919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		whileTest = false;
30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		for(int i = 0; i < 2048; i++)
32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		{
33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			labelBlock[i] = 0;
34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	}
36894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
37894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	VertexProgram::~VertexProgram()
38894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	{
390b65c5e3903233839197fd689895ba57b7f61a5bAlexis Hetu		for(int i = 0; i < VERTEX_TEXTURE_IMAGE_UNITS; i++)
40894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		{
41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			delete sampler[i];
42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	}
44894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	void VertexProgram::pipeline(Registers &r)
46894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	{
470b65c5e3903233839197fd689895ba57b7f61a5bAlexis Hetu		for(int i = 0; i < VERTEX_TEXTURE_IMAGE_UNITS; i++)
48894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		{
49894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			sampler[i] = new SamplerCore(r.constants, state.samplerState[i]);
50894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
51894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
52894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		if(!state.preTransformed)
53894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		{
5419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			program(r);
55894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
56894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		else
57894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		{
58894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			passThrough(r);
59894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
60894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	}
61894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	void VertexProgram::program(Registers &r)
63894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	{
6419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	//	shader->print("VertexShader-%0.8X.txt", state.shaderID);
65894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		unsigned short version = shader->getVersion();
67894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		r.enableIndex = 0;
6919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		r.stackIndex = 0;
70894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
714677a5f7a837e06dac90521eee0cf063cbfb9c9bNicolas Capens		if(shader->containsLeaveInstruction())
724677a5f7a837e06dac90521eee0cf063cbfb9c9bNicolas Capens		{
734677a5f7a837e06dac90521eee0cf063cbfb9c9bNicolas Capens			r.enableLeave = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
744677a5f7a837e06dac90521eee0cf063cbfb9c9bNicolas Capens		}
754677a5f7a837e06dac90521eee0cf063cbfb9c9bNicolas Capens
7619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		// Create all call site return blocks up front
77903e025f6cd8d978f013c741431b87b04ae01fd1Alexis Hetu		for(size_t i = 0; i < shader->getLength(); i++)
7819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		{
7919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			const Shader::Instruction *instruction = shader->getInstruction(i);
8019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			Shader::Opcode opcode = instruction->opcode;
81894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			if(opcode == Shader::OPCODE_CALL || opcode == Shader::OPCODE_CALLNZ)
83894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			{
8419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				const Dst &dst = instruction->dst;
85894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				ASSERT(callRetBlock[dst.label].size() == dst.callSite);
8719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				callRetBlock[dst.label].push_back(Nucleus::createBasicBlock());
88894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			}
89894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
9019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
91903e025f6cd8d978f013c741431b87b04ae01fd1Alexis Hetu		for(size_t i = 0; i < shader->getLength(); i++)
92894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		{
9319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			const Shader::Instruction *instruction = shader->getInstruction(i);
9419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			Shader::Opcode opcode = instruction->opcode;
95894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			if(opcode == Shader::OPCODE_DCL || opcode == Shader::OPCODE_DEF || opcode == Shader::OPCODE_DEFI || opcode == Shader::OPCODE_DEFB)
97894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			{
9819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				continue;
99894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			}
100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			Dst dst = instruction->dst;
10219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			Src src0 = instruction->src[0];
10319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			Src src1 = instruction->src[1];
10419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			Src src2 = instruction->src[2];
105c3d95f36258143af6cca041ede521995d8f890eaAlexis Hetu			Src src3 = instruction->src[3];
10625d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu			Src src4 = instruction->src[4];
107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			bool predicate = instruction->predicate;
10919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			Control control = instruction->control;
11019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			bool integer = dst.type == Shader::PARAMETER_ADDR;
11119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			bool pp = dst.partialPrecision;
112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			Vector4f d;
11419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			Vector4f s0;
11519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			Vector4f s1;
11619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			Vector4f s2;
117c3d95f36258143af6cca041ede521995d8f890eaAlexis Hetu			Vector4f s3;
11825d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu			Vector4f s4;
119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
120af1970ce7685ea66dd02ac462d6f2723d6f6e500Alexis Hetu			if(src0.type != Shader::PARAMETER_VOID) s0 = fetchRegisterF(r, src0);
121af1970ce7685ea66dd02ac462d6f2723d6f6e500Alexis Hetu			if(src1.type != Shader::PARAMETER_VOID) s1 = fetchRegisterF(r, src1);
122af1970ce7685ea66dd02ac462d6f2723d6f6e500Alexis Hetu			if(src2.type != Shader::PARAMETER_VOID) s2 = fetchRegisterF(r, src2);
123c3d95f36258143af6cca041ede521995d8f890eaAlexis Hetu			if(src3.type != Shader::PARAMETER_VOID) s3 = fetchRegisterF(r, src3);
12425d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu			if(src4.type != Shader::PARAMETER_VOID) s4 = fetchRegisterF(r, src4);
125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			switch(opcode)
127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			{
12819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_VS_1_0:										break;
12919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_VS_1_1:										break;
13019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_VS_2_0:										break;
13119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_VS_2_x:										break;
13219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_VS_2_sw:									break;
13319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_VS_3_0:										break;
13419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_VS_3_sw:									break;
13519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_DCL:										break;
13619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_DEF:										break;
13719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_DEFI:										break;
13819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_DEFB:										break;
13919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_NOP:										break;
14019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_ABS:		abs(d, s0);						break;
14119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_ADD:		add(d, s0, s1);					break;
1428d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_IADD:       iadd(d, s0, s1);                break;
14319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_CRS:		crs(d, s0, s1);					break;
14419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_FORWARD1:	forward1(d, s0, s1, s2);		break;
14519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_FORWARD2:	forward2(d, s0, s1, s2);		break;
14619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_FORWARD3:	forward3(d, s0, s1, s2);		break;
14719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_FORWARD4:	forward4(d, s0, s1, s2);		break;
14819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_REFLECT1:	reflect1(d, s0, s1);			break;
14919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_REFLECT2:	reflect2(d, s0, s1);			break;
15019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_REFLECT3:	reflect3(d, s0, s1);			break;
15119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_REFLECT4:	reflect4(d, s0, s1);			break;
15219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_REFRACT1:	refract1(d, s0, s1, s2.x);		break;
15319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_REFRACT2:	refract2(d, s0, s1, s2.x);		break;
15419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_REFRACT3:	refract3(d, s0, s1, s2.x);		break;
15519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_REFRACT4:	refract4(d, s0, s1, s2.x);		break;
15619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_DP1:		dp1(d, s0, s1);					break;
15719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_DP2:		dp2(d, s0, s1);					break;
15819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_DP3:		dp3(d, s0, s1);					break;
15919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_DP4:		dp4(d, s0, s1);					break;
160c3d95f36258143af6cca041ede521995d8f890eaAlexis Hetu			case Shader::OPCODE_DET2:       det2(d, s0, s1);                break;
161c3d95f36258143af6cca041ede521995d8f890eaAlexis Hetu			case Shader::OPCODE_DET3:       det3(d, s0, s1, s2);            break;
162c3d95f36258143af6cca041ede521995d8f890eaAlexis Hetu			case Shader::OPCODE_DET4:       det4(d, s0, s1, s2, s3);        break;
16319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_ATT:		att(d, s0, s1);					break;
16419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_EXP2X:		exp2x(d, s0, pp);				break;
16519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_EXP2:		exp2(d, s0, pp);				break;
16619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_EXPP:		expp(d, s0, version);			break;
16719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_EXP:		exp(d, s0, pp);					break;
16819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_FRC:		frc(d, s0);						break;
16919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_TRUNC:      trunc(d, s0);                   break;
17019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_FLOOR:      floor(d, s0);                   break;
171af1970ce7685ea66dd02ac462d6f2723d6f6e500Alexis Hetu			case Shader::OPCODE_ROUND:      round(d, s0);                   break;
1728e851c1a0b9e923ceb77751b7ae7221d22833bcbAlexis Hetu			case Shader::OPCODE_ROUNDEVEN:	roundEven(d, s0);               break;
17319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_CEIL:       ceil(d, s0);                    break;
17419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_LIT:		lit(d, s0);						break;
17519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_LOG2X:		log2x(d, s0, pp);				break;
17619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_LOG2:		log2(d, s0, pp);				break;
17719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_LOGP:		logp(d, s0, version);			break;
17819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_LOG:		log(d, s0, pp);					break;
17919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_LRP:		lrp(d, s0, s1, s2);				break;
18019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_STEP:		step(d, s0, s1);				break;
18119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_SMOOTH:		smooth(d, s0, s1, s2);			break;
1828d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_FLOATBITSTOINT:
1838d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_FLOATBITSTOUINT:
1848d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_INTBITSTOFLOAT:
1858d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_UINTBITSTOFLOAT: d = s0;                    break;
18619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_M3X2:		M3X2(r, d, s0, src1);			break;
18719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_M3X3:		M3X3(r, d, s0, src1);			break;
18819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_M3X4:		M3X4(r, d, s0, src1);			break;
18919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_M4X3:		M4X3(r, d, s0, src1);			break;
19019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_M4X4:		M4X4(r, d, s0, src1);			break;
19119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_MAD:		mad(d, s0, s1, s2);				break;
1928d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_IMAD:		imad(d, s0, s1, s2);			break;
19319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_MAX:		max(d, s0, s1);					break;
1948d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_IMAX:       imax(d, s0, s1);                break;
1958d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_UMAX:       umax(d, s0, s1);                break;
19619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_MIN:		min(d, s0, s1);					break;
1978d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_IMIN:       imin(d, s0, s1);                break;
1988d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_UMIN:       umin(d, s0, s1);                break;
19919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_MOV:		mov(d, s0, integer);			break;
20002a2bb807c1e947bc99d68ba7dd4984289278be0Alexis Hetu			case Shader::OPCODE_MOVA:       mov(d, s0, true);               break;
2018d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_NEG:        neg(d, s0);                     break;
2028d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_INEG:       ineg(d, s0);                    break;
20319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_F2B:		f2b(d, s0);						break;
20419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_B2F:		b2f(d, s0);						break;
2058d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_F2I:        f2i(d, s0);                     break;
2068d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_I2F:        i2f(d, s0);                     break;
2078d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_F2U:        f2u(d, s0);                     break;
2088d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_U2F:        u2f(d, s0);                     break;
2098d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_I2B:        i2b(d, s0);                     break;
2108d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_B2I:        b2i(d, s0);                     break;
2118d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_U2B:        u2b(d, s0);                     break;
2128d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_B2U:        b2u(d, s0);                     break;
21319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_MUL:		mul(d, s0, s1);					break;
2148d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_IMUL:       imul(d, s0, s1);                break;
21519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_NRM2:		nrm2(d, s0, pp);				break;
21619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_NRM3:		nrm3(d, s0, pp);				break;
21719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_NRM4:		nrm4(d, s0, pp);				break;
21819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_POWX:		powx(d, s0, s1, pp);			break;
21919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_POW:		pow(d, s0, s1, pp);				break;
22019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_RCPX:		rcpx(d, s0, pp);				break;
22119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_DIV:		div(d, s0, s1);					break;
2228d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_IDIV:       idiv(d, s0, s1);                break;
2238d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_UDIV:       udiv(d, s0, s1);                break;
22419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_MOD:		mod(d, s0, s1);					break;
2258d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_IMOD:       imod(d, s0, s1);                break;
2268d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_UMOD:       umod(d, s0, s1);                break;
2278d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_SHL:        shl(d, s0, s1);                 break;
2288d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_ISHR:       ishr(d, s0, s1);                 break;
2298d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_USHR:       ushr(d, s0, s1);                 break;
23019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_RSQX:		rsqx(d, s0, pp);				break;
23119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_SQRT:		sqrt(d, s0, pp);				break;
23219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_RSQ:		rsq(d, s0, pp);					break;
23319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_LEN2:		len2(d.x, s0, pp);				break;
23419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_LEN3:		len3(d.x, s0, pp);				break;
23519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_LEN4:		len4(d.x, s0, pp);				break;
23619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_DIST1:		dist1(d.x, s0, s1, pp);			break;
23719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_DIST2:		dist2(d.x, s0, s1, pp);			break;
23819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_DIST3:		dist3(d.x, s0, s1, pp);			break;
23919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_DIST4:		dist4(d.x, s0, s1, pp);			break;
24019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_SGE:		step(d, s1, s0);				break;
24119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_SGN:		sgn(d, s0);						break;
24219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_SINCOS:		sincos(d, s0, pp);				break;
24319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_COS:		cos(d, s0, pp);					break;
24419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_SIN:		sin(d, s0, pp);					break;
24519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_TAN:		tan(d, s0);						break;
24619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_ACOS:		acos(d, s0);					break;
24719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_ASIN:		asin(d, s0);					break;
24819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_ATAN:		atan(d, s0);					break;
24919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_ATAN2:		atan2(d, s0, s1);				break;
250af1970ce7685ea66dd02ac462d6f2723d6f6e500Alexis Hetu			case Shader::OPCODE_COSH:		cosh(d, s0, pp);				break;
251af1970ce7685ea66dd02ac462d6f2723d6f6e500Alexis Hetu			case Shader::OPCODE_SINH:		sinh(d, s0, pp);				break;
252af1970ce7685ea66dd02ac462d6f2723d6f6e500Alexis Hetu			case Shader::OPCODE_TANH:		tanh(d, s0, pp);				break;
253af1970ce7685ea66dd02ac462d6f2723d6f6e500Alexis Hetu			case Shader::OPCODE_ACOSH:		acosh(d, s0, pp);				break;
254af1970ce7685ea66dd02ac462d6f2723d6f6e500Alexis Hetu			case Shader::OPCODE_ASINH:		asinh(d, s0, pp);				break;
255af1970ce7685ea66dd02ac462d6f2723d6f6e500Alexis Hetu			case Shader::OPCODE_ATANH:		atanh(d, s0, pp);				break;
25619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_SLT:		slt(d, s0, s1);					break;
25719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_SUB:		sub(d, s0, s1);					break;
2588d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_ISUB:       isub(d, s0, s1);                break;
25919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_BREAK:		BREAK(r);						break;
26019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_BREAKC:		BREAKC(r, s0, s1, control);		break;
26119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_BREAKP:		BREAKP(r, src0);				break;
26219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_CONTINUE:	CONTINUE(r);					break;
26319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_TEST:		TEST();							break;
26419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_CALL:		CALL(r, dst.label, dst.callSite);         break;
26519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_CALLNZ:		CALLNZ(r, dst.label, dst.callSite, src0); break;
26619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_ELSE:		ELSE(r);						break;
26719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_ENDIF:		ENDIF(r);						break;
26819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_ENDLOOP:	ENDLOOP(r);						break;
26919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_ENDREP:		ENDREP(r);						break;
27019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_ENDWHILE:	ENDWHILE(r);					break;
27119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_IF:			IF(r, src0);					break;
27219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_IFC:		IFC(r, s0, s1, control);		break;
27319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_LABEL:		LABEL(dst.index);				break;
27419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_LOOP:		LOOP(r, src1);					break;
27519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_REP:		REP(r, src0);					break;
27619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_WHILE:		WHILE(r, src0);					break;
27719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_RET:		RET(r);							break;
27819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_LEAVE:		LEAVE(r);						break;
27919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_CMP:		cmp(d, s0, s1, control);		break;
28019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_ICMP:		icmp(d, s0, s1, control);		break;
2818d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_UCMP:       ucmp(d, s0, s1, control);       break;
28219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_SELECT:		select(d, s0, s1, s2);			break;
28319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_EXTRACT:	extract(d.x, s0, s1.x);			break;
28419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_INSERT:		insert(d, s0, s1.x, s2.x);		break;
28519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_ALL:		all(d.x, s0);					break;
28619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_ANY:		any(d.x, s0);					break;
28719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_NOT:		not(d, s0);						break;
2888d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_OR:         or(d, s0, s1);                  break;
2898d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_XOR:        xor(d, s0, s1);                 break;
2908d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_AND:        and(d, s0, s1);                 break;
2918d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_EQ:         equal(d, s0, s1);               break;
2928d78cf77ce7c6bc4ba320fefb2cd2b74cc408b1aAlexis Hetu			case Shader::OPCODE_NE:         notEqual(d, s0, s1);            break;
29319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_TEXLDL:		TEXLDL(r, d, s0, src1);			break;
29419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_TEX:		TEX(r, d, s0, src1);			break;
29525d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu			case Shader::OPCODE_TEXOFFSET:  TEXOFFSET(r, d, s0, src1, s2, s3);        break;
29625d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu			case Shader::OPCODE_TEXLDLOFFSET: TEXLDL(r, d, s0, src1, s2);	break;
29725d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu			case Shader::OPCODE_TEXELFETCH: TEXELFETCH(r, d, s0, src1, s2);           break;
29825d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu			case Shader::OPCODE_TEXELFETCHOFFSET: TEXELFETCH(r, d, s0, src1, s2, s3); break;
29925d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu			case Shader::OPCODE_TEXGRAD:    TEXGRAD(r, d, s0, src1, s2, s3);          break;
30025d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu			case Shader::OPCODE_TEXGRADOFFSET: TEXGRAD(r, d, s0, src1, s2, s3, s4);   break;
3019bcb31da0cf95258949da1bb697f3981a70c476bAlexis Hetu			case Shader::OPCODE_TEXSIZE:	TEXSIZE(r, d, s0.x, src1);		break;
30219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			case Shader::OPCODE_END:										break;
303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			default:
304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman				ASSERT(false);
305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			}
306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
30719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			if(dst.type != Shader::PARAMETER_VOID && dst.type != Shader::PARAMETER_LABEL && opcode != Shader::OPCODE_NOP)
308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			{
30919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				if(dst.integer)
31019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				{
31119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					switch(opcode)
31219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					{
31319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					case Shader::OPCODE_DIV:
31419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						if(dst.x) d.x = Trunc(d.x);
31519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						if(dst.y) d.y = Trunc(d.y);
31619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						if(dst.z) d.z = Trunc(d.z);
31719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						if(dst.w) d.w = Trunc(d.w);
31819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						break;
31919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					default:
32019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						break;   // No truncation to integer required when arguments are integer
32119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					}
32219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				}
32319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
32419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				if(dst.saturate)
325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman				{
32619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					if(dst.x) d.x = Max(d.x, Float4(0.0f));
32719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					if(dst.y) d.y = Max(d.y, Float4(0.0f));
32819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					if(dst.z) d.z = Max(d.z, Float4(0.0f));
32919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					if(dst.w) d.w = Max(d.w, Float4(0.0f));
33019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
33119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					if(dst.x) d.x = Min(d.x, Float4(1.0f));
33219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					if(dst.y) d.y = Min(d.y, Float4(1.0f));
33319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					if(dst.z) d.z = Min(d.z, Float4(1.0f));
33419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					if(dst.w) d.w = Min(d.w, Float4(1.0f));
335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman				}
336894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
337c6e8ab13ecf20217229de8f4f19a14bbc18778f9Nicolas Capens				if(instruction->isPredicated())
338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman				{
33919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					Vector4f pDst;   // FIXME: Rename
340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
34119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					switch(dst.type)
342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					{
34319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					case Shader::PARAMETER_VOID:																		break;
34419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					case Shader::PARAMETER_TEMP:
34519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						if(dst.rel.type == Shader::PARAMETER_VOID)
34619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						{
34719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.x) pDst.x = r.r[dst.index].x;
34819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.y) pDst.y = r.r[dst.index].y;
34919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.z) pDst.z = r.r[dst.index].z;
35019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.w) pDst.w = r.r[dst.index].w;
35119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						}
35219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						else
35319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						{
35419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							Int a = relativeAddress(r, dst);
35519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
35619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.x) pDst.x = r.r[dst.index + a].x;
35719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.y) pDst.y = r.r[dst.index + a].y;
35819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.z) pDst.z = r.r[dst.index + a].z;
35919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.w) pDst.w = r.r[dst.index + a].w;
36019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						}
36119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						break;
36219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					case Shader::PARAMETER_ADDR:		pDst = r.a0;													break;
36319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					case Shader::PARAMETER_RASTOUT:
36419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						switch(dst.index)
365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman						{
366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman						case 0:
36719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.x) pDst.x = r.o[Pos].x;
36819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.y) pDst.y = r.o[Pos].y;
36919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.z) pDst.z = r.o[Pos].z;
37019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.w) pDst.w = r.o[Pos].w;
371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman							break;
372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman						case 1:
37319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							pDst.x = r.o[Fog].x;
374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman							break;
375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman						case 2:
37619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							pDst.x = r.o[Pts].y;
377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman							break;
378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman						default:
379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman							ASSERT(false);
380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman						}
381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman						break;
38219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					case Shader::PARAMETER_ATTROUT:
38319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						if(dst.x) pDst.x = r.o[D0 + dst.index].x;
38419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						if(dst.y) pDst.y = r.o[D0 + dst.index].y;
38519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						if(dst.z) pDst.z = r.o[D0 + dst.index].z;
38619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						if(dst.w) pDst.w = r.o[D0 + dst.index].w;
387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman						break;
38819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					case Shader::PARAMETER_TEXCRDOUT:
38919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				//	case Shader::PARAMETER_OUTPUT:
390894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman						if(version < 0x0300)
391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman						{
39219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.x) pDst.x = r.o[T0 + dst.index].x;
39319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.y) pDst.y = r.o[T0 + dst.index].y;
39419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.z) pDst.z = r.o[T0 + dst.index].z;
39519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.w) pDst.w = r.o[T0 + dst.index].w;
396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman						}
397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman						else
398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman						{
39919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.rel.type == Shader::PARAMETER_VOID)   // Not relative
400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman							{
40119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman								if(dst.x) pDst.x = r.o[dst.index].x;
40219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman								if(dst.y) pDst.y = r.o[dst.index].y;
40319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman								if(dst.z) pDst.z = r.o[dst.index].z;
40419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman								if(dst.w) pDst.w = r.o[dst.index].w;
405894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman							}
40619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							else if(dst.rel.type == Shader::PARAMETER_LOOP)
407894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman							{
408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman								Int aL = r.aL[r.loopDepth];
409894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
41019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman								if(dst.x) pDst.x = r.o[dst.index + aL].x;
41119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman								if(dst.y) pDst.y = r.o[dst.index + aL].y;
41219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman								if(dst.z) pDst.z = r.o[dst.index + aL].z;
41319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman								if(dst.w) pDst.w = r.o[dst.index + aL].w;
41419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							}
41519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							else
41619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							{
41719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman								Int a = relativeAddress(r, dst);
41819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
41919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman								if(dst.x) pDst.x = r.o[dst.index + a].x;
42019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman								if(dst.y) pDst.y = r.o[dst.index + a].y;
42119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman								if(dst.z) pDst.z = r.o[dst.index + a].z;
42219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman								if(dst.w) pDst.w = r.o[dst.index + a].w;
423894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman							}
424894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman						}
425894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman						break;
42619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					case Shader::PARAMETER_LABEL:																		break;
42719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					case Shader::PARAMETER_PREDICATE:	pDst = r.p0;													break;
42819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					case Shader::PARAMETER_INPUT:																		break;
429894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					default:
430894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman						ASSERT(false);
431894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					}
432894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
43319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					Int4 enable = enableMask(r, instruction);
434894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
435894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					Int4 xEnable = enable;
436894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					Int4 yEnable = enable;
437894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					Int4 zEnable = enable;
438894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					Int4 wEnable = enable;
439894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
440894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					if(predicate)
441894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					{
44219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						unsigned char pSwizzle = instruction->predicateSwizzle;
443894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
444894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman						Float4 xPredicate = r.p0[(pSwizzle >> 0) & 0x03];
445894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman						Float4 yPredicate = r.p0[(pSwizzle >> 2) & 0x03];
446894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman						Float4 zPredicate = r.p0[(pSwizzle >> 4) & 0x03];
447894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman						Float4 wPredicate = r.p0[(pSwizzle >> 6) & 0x03];
448894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
44919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						if(!instruction->predicateNot)
450894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman						{
45119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.x) xEnable = xEnable & As<Int4>(xPredicate);
45219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.y) yEnable = yEnable & As<Int4>(yPredicate);
45319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.z) zEnable = zEnable & As<Int4>(zPredicate);
45419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.w) wEnable = wEnable & As<Int4>(wPredicate);
455894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman						}
456894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman						else
457894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman						{
45819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.x) xEnable = xEnable & ~As<Int4>(xPredicate);
45919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.y) yEnable = yEnable & ~As<Int4>(yPredicate);
46019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.z) zEnable = zEnable & ~As<Int4>(zPredicate);
46119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.w) wEnable = wEnable & ~As<Int4>(wPredicate);
462894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman						}
463894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					}
464894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
46519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					if(dst.x) d.x = As<Float4>(As<Int4>(d.x) & xEnable);
46619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					if(dst.y) d.y = As<Float4>(As<Int4>(d.y) & yEnable);
46719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					if(dst.z) d.z = As<Float4>(As<Int4>(d.z) & zEnable);
46819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					if(dst.w) d.w = As<Float4>(As<Int4>(d.w) & wEnable);
469894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
47019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					if(dst.x) d.x = As<Float4>(As<Int4>(d.x) | (As<Int4>(pDst.x) & ~xEnable));
47119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					if(dst.y) d.y = As<Float4>(As<Int4>(d.y) | (As<Int4>(pDst.y) & ~yEnable));
47219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					if(dst.z) d.z = As<Float4>(As<Int4>(d.z) | (As<Int4>(pDst.z) & ~zEnable));
47319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					if(dst.w) d.w = As<Float4>(As<Int4>(d.w) | (As<Int4>(pDst.w) & ~wEnable));
474894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman				}
475894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
47619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				switch(dst.type)
477894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman				{
47819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				case Shader::PARAMETER_VOID:
479894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					break;
48019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				case Shader::PARAMETER_TEMP:
48119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					if(dst.rel.type == Shader::PARAMETER_VOID)
48219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					{
48319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						if(dst.x) r.r[dst.index].x = d.x;
48419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						if(dst.y) r.r[dst.index].y = d.y;
48519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						if(dst.z) r.r[dst.index].z = d.z;
48619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						if(dst.w) r.r[dst.index].w = d.w;
48719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					}
48819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					else
48919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					{
49019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						Int a = relativeAddress(r, dst);
49119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
49219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						if(dst.x) r.r[dst.index + a].x = d.x;
49319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						if(dst.y) r.r[dst.index + a].y = d.y;
49419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						if(dst.z) r.r[dst.index + a].z = d.z;
49519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						if(dst.w) r.r[dst.index + a].w = d.w;
49619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					}
497894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					break;
49819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				case Shader::PARAMETER_ADDR:
49919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					if(dst.x) r.a0.x = d.x;
50019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					if(dst.y) r.a0.y = d.y;
50119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					if(dst.z) r.a0.z = d.z;
50219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					if(dst.w) r.a0.w = d.w;
503894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					break;
50419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				case Shader::PARAMETER_RASTOUT:
50519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					switch(dst.index)
506894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					{
507894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					case 0:
50819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						if(dst.x) r.o[Pos].x = d.x;
50919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						if(dst.y) r.o[Pos].y = d.y;
51019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						if(dst.z) r.o[Pos].z = d.z;
51119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						if(dst.w) r.o[Pos].w = d.w;
512894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman						break;
513894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					case 1:
51419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						r.o[Fog].x = d.x;
515894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman						break;
516894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					case 2:
51719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						r.o[Pts].y = d.x;
518894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman						break;
519894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					default:	ASSERT(false);
520894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					}
521894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					break;
52219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				case Shader::PARAMETER_ATTROUT:
52319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					if(dst.x) r.o[D0 + dst.index].x = d.x;
52419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					if(dst.y) r.o[D0 + dst.index].y = d.y;
52519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					if(dst.z) r.o[D0 + dst.index].z = d.z;
52619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					if(dst.w) r.o[D0 + dst.index].w = d.w;
527894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					break;
52819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				case Shader::PARAMETER_TEXCRDOUT:
52919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			//	case Shader::PARAMETER_OUTPUT:
530894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					if(version < 0x0300)
531894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					{
53219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						if(dst.x) r.o[T0 + dst.index].x = d.x;
53319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						if(dst.y) r.o[T0 + dst.index].y = d.y;
53419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						if(dst.z) r.o[T0 + dst.index].z = d.z;
53519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						if(dst.w) r.o[T0 + dst.index].w = d.w;
536894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					}
537894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					else
538894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					{
53919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						if(dst.rel.type == Shader::PARAMETER_VOID)   // Not relative
540894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman						{
54119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.x) r.o[dst.index].x = d.x;
54219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.y) r.o[dst.index].y = d.y;
54319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.z) r.o[dst.index].z = d.z;
54419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.w) r.o[dst.index].w = d.w;
545894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman						}
54619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						else if(dst.rel.type == Shader::PARAMETER_LOOP)
547894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman						{
548894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman							Int aL = r.aL[r.loopDepth];
549894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
55019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.x) r.o[dst.index + aL].x = d.x;
55119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.y) r.o[dst.index + aL].y = d.y;
55219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.z) r.o[dst.index + aL].z = d.z;
55319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.w) r.o[dst.index + aL].w = d.w;
55419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						}
55519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						else
55619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						{
55719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							Int a = relativeAddress(r, dst);
55819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
55919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.x) r.o[dst.index + a].x = d.x;
56019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.y) r.o[dst.index + a].y = d.y;
56119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.z) r.o[dst.index + a].z = d.z;
56219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							if(dst.w) r.o[dst.index + a].w = d.w;
563894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman						}
564894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					}
565894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					break;
56619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				case Shader::PARAMETER_LABEL:																		break;
56719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				case Shader::PARAMETER_PREDICATE:	r.p0 = d;														break;
56819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				case Shader::PARAMETER_INPUT:																		break;
569894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman				default:
570894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					ASSERT(false);
571894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman				}
572894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			}
573894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
574894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
57519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		if(currentLabel != -1)
576894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		{
577894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			Nucleus::setInsertBlock(returnBlock);
578894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
579894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	}
580894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
581894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	void VertexProgram::passThrough(Registers &r)
582894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	{
58319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		if(shader)
584894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		{
585894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			for(int i = 0; i < 12; i++)
586894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			{
58719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				unsigned char usage = shader->output[i][0].usage;
588894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
589894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman				switch(usage)
590894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman				{
591894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman				case 0xFF:
592894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					continue;
59319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				case Shader::USAGE_PSIZE:
59419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					r.o[i].y = r.v[i].x;
595894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					break;
59619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				case Shader::USAGE_TEXCOORD:
59719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					r.o[i].x = r.v[i].x;
59819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					r.o[i].y = r.v[i].y;
59919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					r.o[i].z = r.v[i].z;
60019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					r.o[i].w = r.v[i].w;
601894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					break;
60219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				case Shader::USAGE_POSITION:
60319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					r.o[i].x = r.v[i].x;
60419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					r.o[i].y = r.v[i].y;
60519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					r.o[i].z = r.v[i].z;
60619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					r.o[i].w = r.v[i].w;
607894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					break;
60819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				case Shader::USAGE_COLOR:
60919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					r.o[i].x = r.v[i].x;
61019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					r.o[i].y = r.v[i].y;
61119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					r.o[i].z = r.v[i].z;
61219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					r.o[i].w = r.v[i].w;
613894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					break;
61419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				case Shader::USAGE_FOG:
61519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					r.o[i].x = r.v[i].x;
616894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					break;
617894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman				default:
618894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman					ASSERT(false);
619894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman				}
620894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			}
621894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
622894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		else
623894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		{
62419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			r.o[Pos].x = r.v[PositionT].x;
62519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			r.o[Pos].y = r.v[PositionT].y;
62619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			r.o[Pos].z = r.v[PositionT].z;
62719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			r.o[Pos].w = r.v[PositionT].w;
628894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
629894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			for(int i = 0; i < 2; i++)
630894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			{
63119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				r.o[D0 + i].x = r.v[Color0 + i].x;
63219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				r.o[D0 + i].y = r.v[Color0 + i].y;
63319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				r.o[D0 + i].z = r.v[Color0 + i].z;
63419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				r.o[D0 + i].w = r.v[Color0 + i].w;
635894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			}
636894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
637894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			for(int i = 0; i < 8; i++)
638894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			{
63919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				r.o[T0 + i].x = r.v[TexCoord0 + i].x;
64019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				r.o[T0 + i].y = r.v[TexCoord0 + i].y;
64119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				r.o[T0 + i].z = r.v[TexCoord0 + i].z;
64219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				r.o[T0 + i].w = r.v[TexCoord0 + i].w;
643894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			}
644894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
64566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman			r.o[Pts].y = r.v[PointSize].x;
646894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
647894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	}
648894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
649af1970ce7685ea66dd02ac462d6f2723d6f6e500Alexis Hetu	Vector4f VertexProgram::fetchRegisterF(Registers &r, const Src &src, int offset)
650894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	{
651894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		int i = src.index + offset;
652894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
65319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		Vector4f reg;
654894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
655894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		switch(src.type)
656894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		{
65719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		case Shader::PARAMETER_TEMP:
65819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			if(src.rel.type == Shader::PARAMETER_VOID)
65919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			{
66019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				reg = r.r[i];
66119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			}
66219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			else
66319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			{
66419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				reg = r.r[i + relativeAddress(r, src)];
66519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			}
66619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			break;
66719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		case Shader::PARAMETER_CONST:
66819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			reg = readConstant(r, src, offset);
66919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			break;
67019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		case Shader::PARAMETER_INPUT:
67119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            if(src.rel.type == Shader::PARAMETER_VOID)
67219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			{
67319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				reg = r.v[i];
67419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			}
67519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			else
67619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			{
67719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				reg = r.v[i + relativeAddress(r, src)];
67819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			}
67919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            break;
68019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		case Shader::PARAMETER_VOID:			return r.r[0];   // Dummy
68119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		case Shader::PARAMETER_FLOAT4LITERAL:
68219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			reg.x = Float4(src.value[0]);
68319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			reg.y = Float4(src.value[1]);
68419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			reg.z = Float4(src.value[2]);
68519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			reg.w = Float4(src.value[3]);
68619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			break;
68719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		case Shader::PARAMETER_ADDR:			reg = r.a0;		break;
68819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		case Shader::PARAMETER_CONSTBOOL:		return r.r[0];   // Dummy
68919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		case Shader::PARAMETER_CONSTINT:		return r.r[0];   // Dummy
69019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		case Shader::PARAMETER_LOOP:			return r.r[0];   // Dummy
69119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		case Shader::PARAMETER_PREDICATE:		return r.r[0];   // Dummy
69219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		case Shader::PARAMETER_SAMPLER:
69319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			if(src.rel.type == Shader::PARAMETER_VOID)
69419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			{
69519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				reg.x = As<Float4>(Int4(i));
69619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			}
69719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			else if(src.rel.type == Shader::PARAMETER_TEMP)
69819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			{
69902a2bb807c1e947bc99d68ba7dd4984289278be0Alexis Hetu				reg.x = As<Float4>(Int4(i) + As<Int4>(r.r[src.rel.index].x));
70019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			}
70119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			return reg;
70219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		case Shader::PARAMETER_OUTPUT:
70319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            if(src.rel.type == Shader::PARAMETER_VOID)
70419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			{
70519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				reg = r.o[i];
70619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			}
70719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			else
70819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			{
70919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				reg = r.o[i + relativeAddress(r, src)];
71019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			}
71119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			break;
712dd8df68e64365aafe91893b11bf7cc4b67599ed4Alexis Hetu		case Shader::PARAMETER_MISCTYPE:
71302a2bb807c1e947bc99d68ba7dd4984289278be0Alexis Hetu			reg.x = As<Float>(Int(r.instanceID));
714dd8df68e64365aafe91893b11bf7cc4b67599ed4Alexis Hetu			return reg;
715894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		default:
716894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			ASSERT(false);
717894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
718894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
71966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman		const Float4 &x = reg[(src.swizzle >> 0) & 0x3];
72066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman		const Float4 &y = reg[(src.swizzle >> 2) & 0x3];
72166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman		const Float4 &z = reg[(src.swizzle >> 4) & 0x3];
72266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman		const Float4 &w = reg[(src.swizzle >> 6) & 0x3];
723894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
72466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman		Vector4f mod;
725894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
726894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		switch(src.modifier)
727894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		{
72819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		case Shader::MODIFIER_NONE:
72966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman			mod.x = x;
73066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman			mod.y = y;
73166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman			mod.z = z;
73266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman			mod.w = w;
733894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			break;
73419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		case Shader::MODIFIER_NEGATE:
73566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman			mod.x = -x;
73666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman			mod.y = -y;
73766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman			mod.z = -z;
73866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman			mod.w = -w;
739894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			break;
74019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		case Shader::MODIFIER_ABS:
74166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman			mod.x = Abs(x);
74266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman			mod.y = Abs(y);
74366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman			mod.z = Abs(z);
74466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman			mod.w = Abs(w);
745894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			break;
74619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		case Shader::MODIFIER_ABS_NEGATE:
74766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman			mod.x = -Abs(x);
74866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman			mod.y = -Abs(y);
74966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman			mod.z = -Abs(z);
75066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman			mod.w = -Abs(w);
751894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			break;
75219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		case Shader::MODIFIER_NOT:
75366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman			mod.x = As<Float4>(As<Int4>(x) ^ Int4(0xFFFFFFFF));
75466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman			mod.y = As<Float4>(As<Int4>(y) ^ Int4(0xFFFFFFFF));
75566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman			mod.z = As<Float4>(As<Int4>(z) ^ Int4(0xFFFFFFFF));
75666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman			mod.w = As<Float4>(As<Int4>(w) ^ Int4(0xFFFFFFFF));
757894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			break;
758894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		default:
759894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			ASSERT(false);
760894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
761894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
762894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		return mod;
763894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	}
764894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
76519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	Vector4f VertexProgram::readConstant(Registers &r, const Src &src, int offset)
766894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	{
76719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		Vector4f c;
76819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
76919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		int i = src.index + offset;
77019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
77119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		if(src.rel.type == Shader::PARAMETER_VOID)   // Not relative
77219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		{
77319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]));
77419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
77519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			c.x = c.x.xxxx;
77619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			c.y = c.y.yyyy;
77719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			c.z = c.z.zzzz;
77819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			c.w = c.w.wwww;
77919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
780eafdb22c36f2077815eadf2a1db0fb6547bf0241Nicolas Capens			if(shader->containsDefineInstruction())   // Constant may be known at compile time
78119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			{
782903e025f6cd8d978f013c741431b87b04ae01fd1Alexis Hetu				for(size_t j = 0; j < shader->getLength(); j++)
78319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				{
78419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					const Shader::Instruction &instruction = *shader->getInstruction(j);
78519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
78619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					if(instruction.opcode == Shader::OPCODE_DEF)
78719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					{
78819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						if(instruction.dst.index == i)
78919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						{
79019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							c.x = Float4(instruction.src[0].value[0]);
79119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							c.y = Float4(instruction.src[0].value[1]);
79219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							c.z = Float4(instruction.src[0].value[2]);
79319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							c.w = Float4(instruction.src[0].value[3]);
79419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
79519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman							break;
79619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						}
79719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					}
79819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				}
79919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			}
80019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		}
80119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		else if(src.rel.type == Shader::PARAMETER_LOOP)
80219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		{
80319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			Int loopCounter = r.aL[r.loopDepth];
80419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
80519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]) + loopCounter * 16);
80619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
80719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			c.x = c.x.xxxx;
80819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			c.y = c.y.yyyy;
80919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			c.z = c.z.zzzz;
81019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			c.w = c.w.wwww;
81119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		}
81219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		else
81319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		{
81419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			if(src.rel.deterministic)
81519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			{
81619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				Int a = relativeAddress(r, src);
81719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
81819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]) + a * 16);
81919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
82019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				c.x = c.x.xxxx;
82119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				c.y = c.y.yyyy;
82219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				c.z = c.z.zzzz;
82319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				c.w = c.w.wwww;
82419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			}
82519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			else
82619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			{
82719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				int component = src.rel.swizzle & 0x03;
82819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				Float4 a;
82919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
83019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				switch(src.rel.type)
83119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				{
83219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				case Shader::PARAMETER_ADDR:   a = r.a0[component]; break;
83319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				case Shader::PARAMETER_TEMP:   a = r.r[src.rel.index][component]; break;
83419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				case Shader::PARAMETER_INPUT:  a = r.v[src.rel.index][component]; break;
83519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				case Shader::PARAMETER_OUTPUT: a = r.o[src.rel.index][component]; break;
83666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman				case Shader::PARAMETER_CONST:  a = *Pointer<Float>(r.data + OFFSET(DrawData,vs.c[src.rel.index][component])); break;
83719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				default: ASSERT(false);
83819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				}
83919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
84002a2bb807c1e947bc99d68ba7dd4984289278be0Alexis Hetu				Int4 index = Int4(i) + As<Int4>(a) * Int4(src.rel.scale);
84119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
84219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				index = Min(As<UInt4>(index), UInt4(256));   // Clamp to constant register range, c[256] = {0, 0, 0, 0}
84319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
84419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				Int index0 = Extract(index, 0);
84519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				Int index1 = Extract(index, 1);
84619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				Int index2 = Extract(index, 2);
84719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				Int index3 = Extract(index, 3);
84819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
84919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				c.x = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index0 * 16, 16);
85019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				c.y = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index1 * 16, 16);
85119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				c.z = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index2 * 16, 16);
85219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index3 * 16, 16);
85319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
85419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				transpose4x4(c.x, c.y, c.z, c.w);
85519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			}
85619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		}
85719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
85819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		return c;
85919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	}
86019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
86119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	Int VertexProgram::relativeAddress(Registers &r, const Shader::Parameter &var)
86219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	{
86319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		ASSERT(var.rel.deterministic);
86419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
86519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		if(var.rel.type == Shader::PARAMETER_TEMP)
86619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		{
86702a2bb807c1e947bc99d68ba7dd4984289278be0Alexis Hetu			return As<Int>(Extract(r.r[var.rel.index].x, 0)) * var.rel.scale;
86819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		}
86919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		else if(var.rel.type == Shader::PARAMETER_INPUT)
87019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		{
87102a2bb807c1e947bc99d68ba7dd4984289278be0Alexis Hetu			return As<Int>(Extract(r.v[var.rel.index].x, 0)) * var.rel.scale;
87219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		}
87319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		else if(var.rel.type == Shader::PARAMETER_OUTPUT)
87419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		{
87502a2bb807c1e947bc99d68ba7dd4984289278be0Alexis Hetu			return As<Int>(Extract(r.o[var.rel.index].x, 0)) * var.rel.scale;
87619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		}
87719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		else if(var.rel.type == Shader::PARAMETER_CONST)
87819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		{
87902a2bb807c1e947bc99d68ba7dd4984289278be0Alexis Hetu			RValue<Int4> c = *Pointer<Int4>(r.data + OFFSET(DrawData, vs.c[var.rel.index]));
88019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
88102a2bb807c1e947bc99d68ba7dd4984289278be0Alexis Hetu			return Extract(c, 0) * var.rel.scale;
88219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		}
88319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		else ASSERT(false);
88419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
88519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		return 0;
88619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	}
88719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
88819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	Int4 VertexProgram::enableMask(Registers &r, const Shader::Instruction *instruction)
88919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	{
89019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		Int4 enable = instruction->analysisBranch ? Int4(r.enableStack[r.enableIndex]) : Int4(0xFFFFFFFF);
891d4ae863d01d5f448dbbba6be4ecc161971a2324fJohn Bauman
892d4ae863d01d5f448dbbba6be4ecc161971a2324fJohn Bauman		if(!whileTest)
89319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		{
894d4ae863d01d5f448dbbba6be4ecc161971a2324fJohn Bauman			if(shader->containsBreakInstruction() && instruction->analysisBreak)
895d4ae863d01d5f448dbbba6be4ecc161971a2324fJohn Bauman			{
896d4ae863d01d5f448dbbba6be4ecc161971a2324fJohn Bauman				enable &= r.enableBreak;
897d4ae863d01d5f448dbbba6be4ecc161971a2324fJohn Bauman			}
89819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
899d4ae863d01d5f448dbbba6be4ecc161971a2324fJohn Bauman			if(shader->containsContinueInstruction() && instruction->analysisContinue)
900d4ae863d01d5f448dbbba6be4ecc161971a2324fJohn Bauman			{
901d4ae863d01d5f448dbbba6be4ecc161971a2324fJohn Bauman				enable &= r.enableContinue;
902d4ae863d01d5f448dbbba6be4ecc161971a2324fJohn Bauman			}
90319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
904d4ae863d01d5f448dbbba6be4ecc161971a2324fJohn Bauman			if(shader->containsLeaveInstruction() && instruction->analysisLeave)
905d4ae863d01d5f448dbbba6be4ecc161971a2324fJohn Bauman			{
906d4ae863d01d5f448dbbba6be4ecc161971a2324fJohn Bauman				enable &= r.enableLeave;
907d4ae863d01d5f448dbbba6be4ecc161971a2324fJohn Bauman			}
90819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		}
90919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
91019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		return enable;
91119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	}
91219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
91319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	void VertexProgram::M3X2(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
91419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	{
915af1970ce7685ea66dd02ac462d6f2723d6f6e500Alexis Hetu		Vector4f row0 = fetchRegisterF(r, src1, 0);
916af1970ce7685ea66dd02ac462d6f2723d6f6e500Alexis Hetu		Vector4f row1 = fetchRegisterF(r, src1, 1);
917894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
918894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		dst.x = dot3(src0, row0);
919894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		dst.y = dot3(src0, row1);
920894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	}
921894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
92219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	void VertexProgram::M3X3(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
923894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	{
924af1970ce7685ea66dd02ac462d6f2723d6f6e500Alexis Hetu		Vector4f row0 = fetchRegisterF(r, src1, 0);
925af1970ce7685ea66dd02ac462d6f2723d6f6e500Alexis Hetu		Vector4f row1 = fetchRegisterF(r, src1, 1);
926af1970ce7685ea66dd02ac462d6f2723d6f6e500Alexis Hetu		Vector4f row2 = fetchRegisterF(r, src1, 2);
927894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
928894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		dst.x = dot3(src0, row0);
929894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		dst.y = dot3(src0, row1);
930894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		dst.z = dot3(src0, row2);
931894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	}
932894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
93319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	void VertexProgram::M3X4(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
934894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	{
935af1970ce7685ea66dd02ac462d6f2723d6f6e500Alexis Hetu		Vector4f row0 = fetchRegisterF(r, src1, 0);
936af1970ce7685ea66dd02ac462d6f2723d6f6e500Alexis Hetu		Vector4f row1 = fetchRegisterF(r, src1, 1);
937af1970ce7685ea66dd02ac462d6f2723d6f6e500Alexis Hetu		Vector4f row2 = fetchRegisterF(r, src1, 2);
938af1970ce7685ea66dd02ac462d6f2723d6f6e500Alexis Hetu		Vector4f row3 = fetchRegisterF(r, src1, 3);
939894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
940894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		dst.x = dot3(src0, row0);
941894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		dst.y = dot3(src0, row1);
942894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		dst.z = dot3(src0, row2);
943894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		dst.w = dot3(src0, row3);
944894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	}
945894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
94619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	void VertexProgram::M4X3(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
947894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	{
948af1970ce7685ea66dd02ac462d6f2723d6f6e500Alexis Hetu		Vector4f row0 = fetchRegisterF(r, src1, 0);
949af1970ce7685ea66dd02ac462d6f2723d6f6e500Alexis Hetu		Vector4f row1 = fetchRegisterF(r, src1, 1);
950af1970ce7685ea66dd02ac462d6f2723d6f6e500Alexis Hetu		Vector4f row2 = fetchRegisterF(r, src1, 2);
951894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
952894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		dst.x = dot4(src0, row0);
953894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		dst.y = dot4(src0, row1);
954894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		dst.z = dot4(src0, row2);
955894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	}
956894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
95719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	void VertexProgram::M4X4(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
958894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	{
959af1970ce7685ea66dd02ac462d6f2723d6f6e500Alexis Hetu		Vector4f row0 = fetchRegisterF(r, src1, 0);
960af1970ce7685ea66dd02ac462d6f2723d6f6e500Alexis Hetu		Vector4f row1 = fetchRegisterF(r, src1, 1);
961af1970ce7685ea66dd02ac462d6f2723d6f6e500Alexis Hetu		Vector4f row2 = fetchRegisterF(r, src1, 2);
962af1970ce7685ea66dd02ac462d6f2723d6f6e500Alexis Hetu		Vector4f row3 = fetchRegisterF(r, src1, 3);
963894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
964894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		dst.x = dot4(src0, row0);
965894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		dst.y = dot4(src0, row1);
966894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		dst.z = dot4(src0, row2);
967894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		dst.w = dot4(src0, row3);
968894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	}
969894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
970894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	void VertexProgram::BREAK(Registers &r)
971894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	{
972894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		llvm::BasicBlock *deadBlock = Nucleus::createBasicBlock();
973894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
974894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
975894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		if(breakDepth == 0)
976894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		{
97719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			r.enableIndex = r.enableIndex - breakDepth;
978894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			Nucleus::createBr(endBlock);
979894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
980894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		else
981894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		{
982894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			r.enableBreak = r.enableBreak & ~r.enableStack[r.enableIndex];
983894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			Bool allBreak = SignMask(r.enableBreak) == 0x0;
984894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
98519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			r.enableIndex = r.enableIndex - breakDepth;
986894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			branch(allBreak, endBlock, deadBlock);
987894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
988894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
989894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		Nucleus::setInsertBlock(deadBlock);
99019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		r.enableIndex = r.enableIndex + breakDepth;
991894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	}
992894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
99319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	void VertexProgram::BREAKC(Registers &r, Vector4f &src0, Vector4f &src1, Control control)
994894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	{
995894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		Int4 condition;
996894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
997894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		switch(control)
998894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		{
99919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		case Shader::CONTROL_GT: condition = CmpNLE(src0.x,  src1.x);	break;
100019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x);		break;
100119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x);	break;
100219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		case Shader::CONTROL_LT: condition = CmpLT(src0.x,  src1.x);	break;
100319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x);	break;
100419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x);		break;
1005894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		default:
1006894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			ASSERT(false);
1007894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
1008894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
100919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		BREAK(r, condition);
1010894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	}
1011894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1012894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	void VertexProgram::BREAKP(Registers &r, const Src &predicateRegister)   // FIXME: Factor out parts common with BREAKC
1013894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	{
1014894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
1015894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
101619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		if(predicateRegister.modifier == Shader::MODIFIER_NOT)
1017894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		{
1018894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			condition = ~condition;
1019894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
1020894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
102119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		BREAK(r, condition);
102219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	}
102319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
102419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	void VertexProgram::BREAK(Registers &r, Int4 &condition)
102519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	{
1026894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		condition &= r.enableStack[r.enableIndex];
1027894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1028894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		llvm::BasicBlock *continueBlock = Nucleus::createBasicBlock();
1029894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
1030894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1031894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		r.enableBreak = r.enableBreak & ~condition;
1032894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		Bool allBreak = SignMask(r.enableBreak) == 0x0;
1033894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
103419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		r.enableIndex = r.enableIndex - breakDepth;
1035894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		branch(allBreak, endBlock, continueBlock);
103619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1037894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		Nucleus::setInsertBlock(continueBlock);
103819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		r.enableIndex = r.enableIndex + breakDepth;
1039894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	}
1040894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
104119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	void VertexProgram::CONTINUE(Registers &r)
104219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	{
104319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		r.enableContinue = r.enableContinue & ~r.enableStack[r.enableIndex];
104419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	}
104519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
104619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	void VertexProgram::TEST()
104719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	{
104819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		whileTest = true;
104919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	}
105019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
105119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	void VertexProgram::CALL(Registers &r, int labelIndex, int callSiteIndex)
1052894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	{
1053894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		if(!labelBlock[labelIndex])
1054894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		{
1055894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			labelBlock[labelIndex] = Nucleus::createBasicBlock();
1056894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
1057894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
105819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		if(callRetBlock[labelIndex].size() > 1)
105919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		{
106019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			r.callStack[r.stackIndex++] = UInt(callSiteIndex);
106119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		}
1062894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
106319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		Int4 restoreLeave = r.enableLeave;
1064894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1065894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		Nucleus::createBr(labelBlock[labelIndex]);
106619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
106719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
106819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		r.enableLeave = restoreLeave;
1069894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	}
1070894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
107119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	void VertexProgram::CALLNZ(Registers &r, int labelIndex, int callSiteIndex, const Src &src)
1072894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	{
107319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		if(src.type == Shader::PARAMETER_CONSTBOOL)
1074894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		{
107519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			CALLNZb(r, labelIndex, callSiteIndex, src);
1076894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
107719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		else if(src.type == Shader::PARAMETER_PREDICATE)
1078894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		{
107919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			CALLNZp(r, labelIndex, callSiteIndex, src);
1080894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
1081894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		else ASSERT(false);
1082894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	}
1083894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
108419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	void VertexProgram::CALLNZb(Registers &r, int labelIndex, int callSiteIndex, const Src &boolRegister)
1085894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	{
1086894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		Bool condition = (*Pointer<Byte>(r.data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0));   // FIXME
1087894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
108819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		if(boolRegister.modifier == Shader::MODIFIER_NOT)
1089894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		{
1090894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			condition = !condition;
1091894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
1092894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1093894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		if(!labelBlock[labelIndex])
1094894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		{
1095894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			labelBlock[labelIndex] = Nucleus::createBasicBlock();
1096894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
1097894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
109819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		if(callRetBlock[labelIndex].size() > 1)
109919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		{
110019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			r.callStack[r.stackIndex++] = UInt(callSiteIndex);
110119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		}
1102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
110319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		Int4 restoreLeave = r.enableLeave;
1104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
110519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		branch(condition, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
110619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
110719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
110819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		r.enableLeave = restoreLeave;
1109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	}
1110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
111119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	void VertexProgram::CALLNZp(Registers &r, int labelIndex, int callSiteIndex, const Src &predicateRegister)
1112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	{
1113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
1114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
111519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		if(predicateRegister.modifier == Shader::MODIFIER_NOT)
1116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		{
1117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			condition = ~condition;
1118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
1119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		condition &= r.enableStack[r.enableIndex];
1121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		if(!labelBlock[labelIndex])
1123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		{
1124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			labelBlock[labelIndex] = Nucleus::createBasicBlock();
1125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
1126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
112719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		if(callRetBlock[labelIndex].size() > 1)
112819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		{
112919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			r.callStack[r.stackIndex++] = UInt(callSiteIndex);
113019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		}
1131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		r.enableIndex++;
1133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		r.enableStack[r.enableIndex] = condition;
113419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		Int4 restoreLeave = r.enableLeave;
1135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
113619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		Bool notAllFalse = SignMask(condition) != 0;
113719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		branch(notAllFalse, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
113819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		r.enableIndex--;
114119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		r.enableLeave = restoreLeave;
1142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	}
1143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	void VertexProgram::ELSE(Registers &r)
1145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	{
1146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		ifDepth--;
1147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		llvm::BasicBlock *falseBlock = ifFalseBlock[ifDepth];
1149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		if(isConditionalIf[ifDepth])
1152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		{
1153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			Int4 condition = ~r.enableStack[r.enableIndex] & r.enableStack[r.enableIndex - 1];
115419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			Bool notAllFalse = SignMask(condition) != 0;
1155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			branch(notAllFalse, falseBlock, endBlock);
1157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			r.enableStack[r.enableIndex] = ~r.enableStack[r.enableIndex] & r.enableStack[r.enableIndex - 1];
1159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
1160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		else
1161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		{
1162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			Nucleus::createBr(endBlock);
1163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			Nucleus::setInsertBlock(falseBlock);
1164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
1165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		ifFalseBlock[ifDepth] = endBlock;
1167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		ifDepth++;
1169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	}
1170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	void VertexProgram::ENDIF(Registers &r)
1172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	{
1173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		ifDepth--;
1174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		llvm::BasicBlock *endBlock = ifFalseBlock[ifDepth];
1176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		Nucleus::createBr(endBlock);
1178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		Nucleus::setInsertBlock(endBlock);
1179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		if(isConditionalIf[ifDepth])
1181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		{
1182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			breakDepth--;
1183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			r.enableIndex--;
1184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
1185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	}
1186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
118719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	void VertexProgram::ENDLOOP(Registers &r)
1188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	{
1189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		loopRepDepth--;
1190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
119119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		r.aL[r.loopDepth] = r.aL[r.loopDepth] + r.increment[r.loopDepth];   // FIXME: +=
119219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		Nucleus::createBr(testBlock);
1197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		Nucleus::setInsertBlock(endBlock);
1198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		r.loopDepth--;
1200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	}
1202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
120319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	void VertexProgram::ENDREP(Registers &r)
1204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	{
1205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		loopRepDepth--;
1206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		Nucleus::createBr(testBlock);
1211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		Nucleus::setInsertBlock(endBlock);
1212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		r.loopDepth--;
1214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	}
1216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
121719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	void VertexProgram::ENDWHILE(Registers &r)
121819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	{
121919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		loopRepDepth--;
122019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
122119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
122219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
122319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
122419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		Nucleus::createBr(testBlock);
122519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		Nucleus::setInsertBlock(endBlock);
122619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
122719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		r.enableIndex--;
122819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
122919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		whileTest = false;
123019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	}
123119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	void VertexProgram::IF(Registers &r, const Src &src)
1233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	{
123419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		if(src.type == Shader::PARAMETER_CONSTBOOL)
1235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		{
1236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			IFb(r, src);
1237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
123819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		else if(src.type == Shader::PARAMETER_PREDICATE)
1239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		{
1240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			IFp(r, src);
1241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
124219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		else
124319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		{
1244af1970ce7685ea66dd02ac462d6f2723d6f6e500Alexis Hetu			Int4 condition = As<Int4>(fetchRegisterF(r, src).x);
124519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			IF(r, condition);
124619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		}
1247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	}
1248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	void VertexProgram::IFb(Registers &r, const Src &boolRegister)
1250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	{
1251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		ASSERT(ifDepth < 24 + 4);
1252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		Bool condition = (*Pointer<Byte>(r.data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0));   // FIXME
1254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
125519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		if(boolRegister.modifier == Shader::MODIFIER_NOT)
1256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		{
125719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			condition = !condition;
1258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
1259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		branch(condition, trueBlock, falseBlock);
1264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		isConditionalIf[ifDepth] = false;
1266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		ifFalseBlock[ifDepth] = falseBlock;
1267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		ifDepth++;
1269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	}
1270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
127119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	void VertexProgram::IFp(Registers &r, const Src &predicateRegister)
1272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	{
1273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
1274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
127519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		if(predicateRegister.modifier == Shader::MODIFIER_NOT)
1276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		{
1277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			condition = ~condition;
1278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
1279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
128019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		IF(r, condition);
1281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	}
1282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
128319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	void VertexProgram::IFC(Registers &r, Vector4f &src0, Vector4f &src1, Control control)
1284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	{
1285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		Int4 condition;
1286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		switch(control)
1288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		{
128919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		case Shader::CONTROL_GT: condition = CmpNLE(src0.x,  src1.x);	break;
129019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x);		break;
129119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x);	break;
129219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		case Shader::CONTROL_LT: condition = CmpLT(src0.x,  src1.x);	break;
129319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x);	break;
129419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x);		break;
1295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		default:
1296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			ASSERT(false);
1297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
1298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
129919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		IF(r, condition);
130019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	}
130119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
130219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	void VertexProgram::IF(Registers &r, Int4 &condition)
130319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	{
1304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		condition &= r.enableStack[r.enableIndex];
1305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		r.enableIndex++;
1307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		r.enableStack[r.enableIndex] = condition;
1308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
131219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		Bool notAllFalse = SignMask(condition) != 0;
1313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		branch(notAllFalse, trueBlock, falseBlock);
1315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1316894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		isConditionalIf[ifDepth] = true;
1317894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		ifFalseBlock[ifDepth] = falseBlock;
1318894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1319894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		ifDepth++;
1320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		breakDepth++;
1321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	}
1322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	void VertexProgram::LABEL(int labelIndex)
1324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	{
132519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		if(!labelBlock[labelIndex])
132619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		{
132719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			labelBlock[labelIndex] = Nucleus::createBasicBlock();
132819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		}
132919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		Nucleus::setInsertBlock(labelBlock[labelIndex]);
133119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		currentLabel = labelIndex;
1332894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	}
1333894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	void VertexProgram::LOOP(Registers &r, const Src &integerRegister)
1335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	{
1336894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		r.loopDepth++;
1337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		r.iteration[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		r.aL[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][1]));
1340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		r.increment[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][2]));
1341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		// FIXME: Compiles to two instructions?
1343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		If(r.increment[r.loopDepth] == 0)
1344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		{
1345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			r.increment[r.loopDepth] = 1;
1346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
1347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		loopRepTestBlock[loopRepDepth] = testBlock;
1353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		loopRepEndBlock[loopRepDepth] = endBlock;
1354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1355894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		// FIXME: jump(testBlock)
1356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		Nucleus::createBr(testBlock);
1357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		Nucleus::setInsertBlock(testBlock);
1358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		branch(r.iteration[r.loopDepth] > 0, loopBlock, endBlock);
1360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		Nucleus::setInsertBlock(loopBlock);
1361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		r.iteration[r.loopDepth] = r.iteration[r.loopDepth] - 1;   // FIXME: --
1363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		loopRepDepth++;
1365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		breakDepth = 0;
1366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	}
1367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	void VertexProgram::REP(Registers &r, const Src &integerRegister)
1369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	{
1370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		r.loopDepth++;
1371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		r.iteration[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		r.aL[r.loopDepth] = r.aL[r.loopDepth - 1];
1374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1376894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		loopRepTestBlock[loopRepDepth] = testBlock;
1380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		loopRepEndBlock[loopRepDepth] = endBlock;
1381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		// FIXME: jump(testBlock)
1383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		Nucleus::createBr(testBlock);
1384894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		Nucleus::setInsertBlock(testBlock);
1385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		branch(r.iteration[r.loopDepth] > 0, loopBlock, endBlock);
1387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		Nucleus::setInsertBlock(loopBlock);
1388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		r.iteration[r.loopDepth] = r.iteration[r.loopDepth] - 1;   // FIXME: --
1390894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		loopRepDepth++;
1392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		breakDepth = 0;
1393894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	}
1394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
139519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	void VertexProgram::WHILE(Registers &r, const Src &temporaryRegister)
139619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	{
139719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		r.enableIndex++;
139819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
139919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
140019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
140119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
140219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
140319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		loopRepTestBlock[loopRepDepth] = testBlock;
140419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		loopRepEndBlock[loopRepDepth] = endBlock;
140519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
140619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		Int4 restoreBreak = r.enableBreak;
140719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		Int4 restoreContinue = r.enableContinue;
140819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
140919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		// FIXME: jump(testBlock)
141019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		Nucleus::createBr(testBlock);
141119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		Nucleus::setInsertBlock(testBlock);
141219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		r.enableContinue = restoreContinue;
141319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1414af1970ce7685ea66dd02ac462d6f2723d6f6e500Alexis Hetu		const Vector4f &src = fetchRegisterF(r, temporaryRegister);
141519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		Int4 condition = As<Int4>(src.x);
141619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		condition &= r.enableStack[r.enableIndex - 1];
141719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		r.enableStack[r.enableIndex] = condition;
141819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
141919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		Bool notAllFalse = SignMask(condition) != 0;
142019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		branch(notAllFalse, loopBlock, endBlock);
142119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
142219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		Nucleus::setInsertBlock(endBlock);
142319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		r.enableBreak = restoreBreak;
142419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
142519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		Nucleus::setInsertBlock(loopBlock);
142619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
142719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		loopRepDepth++;
142819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		breakDepth = 0;
142919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	}
143019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1431894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	void VertexProgram::RET(Registers &r)
1432894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	{
143319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		if(currentLabel == -1)
1434894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		{
1435894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			returnBlock = Nucleus::createBasicBlock();
1436894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			Nucleus::createBr(returnBlock);
1437894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
1438894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		else
1439894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		{
1440894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			llvm::BasicBlock *unreachableBlock = Nucleus::createBasicBlock();
1441894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
144219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			if(callRetBlock[currentLabel].size() > 1)   // Pop the return destination from the call stack
1443894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			{
144419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				// FIXME: Encapsulate
144519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				UInt index = r.callStack[--r.stackIndex];
144619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
144766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman				llvm::Value *value = index.loadValue();
144819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				llvm::Value *switchInst = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
144919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
145019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
145119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				{
145219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					Nucleus::addSwitchCase(switchInst, i, callRetBlock[currentLabel][i]);
145319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				}
145419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			}
145519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			else if(callRetBlock[currentLabel].size() == 1)   // Jump directly to the unique return destination
145619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			{
145719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				Nucleus::createBr(callRetBlock[currentLabel][0]);
145819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			}
145919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			else   // Function isn't called
146019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			{
146119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				Nucleus::createBr(unreachableBlock);
1462894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			}
1463894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1464894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			Nucleus::setInsertBlock(unreachableBlock);
1465894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman			Nucleus::createUnreachable();
1466894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		}
1467894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	}
1468894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
146919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	void VertexProgram::LEAVE(Registers &r)
1470894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	{
147119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		r.enableLeave = r.enableLeave & ~r.enableStack[r.enableIndex];
147219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
147319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		// FIXME: Return from function if all instances left
147419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		// FIXME: Use enableLeave in other control-flow constructs
147519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	}
1476894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
147719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	void VertexProgram::TEXLDL(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
147819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	{
147919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		Vector4f tmp;
148019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		sampleTexture(r, tmp, src1, src0.x, src0.y, src0.z, src0.w);
148119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
148219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		dst.x = tmp[(src1.swizzle >> 0) & 0x3];
148319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		dst.y = tmp[(src1.swizzle >> 2) & 0x3];
148419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		dst.z = tmp[(src1.swizzle >> 4) & 0x3];
148519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		dst.w = tmp[(src1.swizzle >> 6) & 0x3];
148619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	}
1487894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
148819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	void VertexProgram::TEX(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
148919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	{
149019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		Float4 lod = Float4(0.0f);
149119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		Vector4f tmp;
149219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		sampleTexture(r, tmp, src1, src0.x, src0.y, src0.z, lod);
1493894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1494894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		dst.x = tmp[(src1.swizzle >> 0) & 0x3];
1495894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		dst.y = tmp[(src1.swizzle >> 2) & 0x3];
1496894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		dst.z = tmp[(src1.swizzle >> 4) & 0x3];
1497894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman		dst.w = tmp[(src1.swizzle >> 6) & 0x3];
1498894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	}
149919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
150025d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu	void VertexProgram::TEXOFFSET(Registers &r, Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3)
150125d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu	{
150225d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu		UNIMPLEMENTED();
150325d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu	}
150425d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu
150525d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu	void VertexProgram::TEXLDL(Registers &r, Vector4f &dst, Vector4f &src, const Src&, Vector4f &offset)
150625d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu	{
150725d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu		UNIMPLEMENTED();
150825d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu	}
150925d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu
151025d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu	void VertexProgram::TEXELFETCH(Registers &r, Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2)
151125d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu	{
151225d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu		UNIMPLEMENTED();
151325d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu	}
151425d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu
151525d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu	void VertexProgram::TEXELFETCH(Registers &r, Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &offset)
151625d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu	{
151725d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu		UNIMPLEMENTED();
151825d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu	}
151925d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu
152025d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu	void VertexProgram::TEXGRAD(Registers &r, Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3)
152125d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu	{
152225d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu		UNIMPLEMENTED();
152325d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu	}
152425d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu
152525d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu	void VertexProgram::TEXGRAD(Registers &r, Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3, Vector4f &offset)
152625d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu	{
152725d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu		UNIMPLEMENTED();
152825d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu	}
152925d47fc9eb8e8aaf864fab0d6aa7305d034d807bAlexis Hetu
15309bcb31da0cf95258949da1bb697f3981a70c476bAlexis Hetu	void VertexProgram::TEXSIZE(Registers &r, Vector4f &dst, Float4 &lod, const Src &src1)
15319bcb31da0cf95258949da1bb697f3981a70c476bAlexis Hetu	{
15329bcb31da0cf95258949da1bb697f3981a70c476bAlexis Hetu		Pointer<Byte> textureMipmap = r.data + OFFSET(DrawData, mipmap[16]) + src1.index * sizeof(Texture) + OFFSET(Texture, mipmap);
15339bcb31da0cf95258949da1bb697f3981a70c476bAlexis Hetu		for(int i = 0; i < 4; ++i)
15349bcb31da0cf95258949da1bb697f3981a70c476bAlexis Hetu		{
15359bcb31da0cf95258949da1bb697f3981a70c476bAlexis Hetu			Pointer<Byte> mipmap = textureMipmap + (As<Int>(Extract(lod, i)) + Int(1)) * sizeof(Mipmap);
15369bcb31da0cf95258949da1bb697f3981a70c476bAlexis Hetu			dst.x = Insert(dst.x, As<Float>(Int(*Pointer<Short>(mipmap + OFFSET(Mipmap, width)))), i);
15379bcb31da0cf95258949da1bb697f3981a70c476bAlexis Hetu			dst.y = Insert(dst.y, As<Float>(Int(*Pointer<Short>(mipmap + OFFSET(Mipmap, height)))), i);
15389bcb31da0cf95258949da1bb697f3981a70c476bAlexis Hetu			dst.z = Insert(dst.z, As<Float>(Int(*Pointer<Short>(mipmap + OFFSET(Mipmap, depth)))), i);
15399bcb31da0cf95258949da1bb697f3981a70c476bAlexis Hetu		}
15409bcb31da0cf95258949da1bb697f3981a70c476bAlexis Hetu	}
15419bcb31da0cf95258949da1bb697f3981a70c476bAlexis Hetu
154219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	void VertexProgram::sampleTexture(Registers &r, Vector4f &c, const Src &s, Float4 &u, Float4 &v, Float4 &w, Float4 &q)
154319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	{
154419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		if(s.type == Shader::PARAMETER_SAMPLER && s.rel.type == Shader::PARAMETER_VOID)
154519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		{
154619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			Pointer<Byte> texture = r.data + OFFSET(DrawData,mipmap[16]) + s.index * sizeof(Texture);
154719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			sampler[s.index]->sampleTexture(texture, c, u, v, w, q, r.a0, r.a0, false, false, true);
154819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		}
154919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		else
155019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		{
1551af1970ce7685ea66dd02ac462d6f2723d6f6e500Alexis Hetu			Int index = As<Int>(Float(fetchRegisterF(r, s).x.x));
155219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
155319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			for(int i = 0; i < 16; i++)
155419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			{
155519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				if(shader->usesSampler(i))
155619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				{
155719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					If(index == i)
155819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					{
155919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						Pointer<Byte> texture = r.data + OFFSET(DrawData,mipmap[16]) + i * sizeof(Texture);
156019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						sampler[i]->sampleTexture(texture, c, u, v, w, q, r.a0, r.a0, false, false, true);
156119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman						// FIXME: When the sampler states are the same, we could use one sampler and just index the texture
156219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman					}
156319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				}
156419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman			}
156519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		}
156619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	}
1567894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1568