PageRenderTime 63ms CodeModel.GetById 22ms app.highlight 37ms RepoModel.GetById 1ms app.codeStats 1ms

/src/away3d/materials/methods/FilteredShadowMapMethod.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 174 lines | 121 code | 29 blank | 24 comment | 0 complexity | 11e13d80bfd7dd2f5b663cc29c40d4fe MD5 | raw file
  1package away3d.materials.methods
  2{
  3	import away3d.arcane;
  4	import away3d.core.managers.Stage3DProxy;
  5	import away3d.lights.DirectionalLight;
  6	import away3d.materials.compilation.ShaderRegisterCache;
  7	import away3d.materials.compilation.ShaderRegisterElement;
  8	
  9	use namespace arcane;
 10
 11	/**
 12	 * DitheredShadowMapMethod provides a softened shadowing technique by bilinearly interpolating shadow comparison
 13	 * results of neighbouring pixels.
 14	 */
 15	public class FilteredShadowMapMethod extends SimpleShadowMapMethodBase
 16	{
 17		/**
 18		 * Creates a new BasicDiffuseMethod object.
 19		 *
 20		 * @param castingLight The light casting the shadow
 21		 */
 22		public function FilteredShadowMapMethod(castingLight:DirectionalLight)
 23		{
 24			super(castingLight);
 25		}
 26
 27		/**
 28		 * @inheritDoc
 29		 */
 30		override arcane function initConstants(vo:MethodVO):void
 31		{
 32			super.initConstants(vo);
 33			
 34			var fragmentData:Vector.<Number> = vo.fragmentData;
 35			var index:int = vo.fragmentConstantsIndex;
 36			fragmentData[index + 8] = .5;
 37			var size:int = castingLight.shadowMapper.depthMapSize;
 38			fragmentData[index + 9] = size;
 39			fragmentData[index + 10] = 1/size;
 40		}
 41		
 42		/**
 43		 * @inheritDoc
 44		 */
 45		override protected function getPlanarFragmentCode(vo:MethodVO, regCache:ShaderRegisterCache, targetReg:ShaderRegisterElement):String
 46		{
 47			var depthMapRegister:ShaderRegisterElement = regCache.getFreeTextureReg();
 48			var decReg:ShaderRegisterElement = regCache.getFreeFragmentConstant();
 49			var dataReg:ShaderRegisterElement = regCache.getFreeFragmentConstant();
 50			// TODO: not used
 51			dataReg = dataReg;
 52			var customDataReg:ShaderRegisterElement = regCache.getFreeFragmentConstant();
 53			var depthCol:ShaderRegisterElement = regCache.getFreeFragmentVectorTemp();
 54			var uvReg:ShaderRegisterElement;
 55			var code:String = "";
 56			vo.fragmentConstantsIndex = decReg.index*4;
 57			
 58			regCache.addFragmentTempUsages(depthCol, 1);
 59			
 60			uvReg = regCache.getFreeFragmentVectorTemp();
 61			regCache.addFragmentTempUsages(uvReg, 1);
 62			
 63			code += "mov " + uvReg + ", " + _depthMapCoordReg + "\n" +
 64				
 65				"tex " + depthCol + ", " + _depthMapCoordReg + ", " + depthMapRegister + " <2d, nearest, clamp>\n" +
 66				"dp4 " + depthCol + ".z, " + depthCol + ", " + decReg + "\n" +
 67				"slt " + uvReg + ".z, " + _depthMapCoordReg + ".z, " + depthCol + ".z\n" +   // 0 if in shadow
 68				
 69				"add " + uvReg + ".x, " + _depthMapCoordReg + ".x, " + customDataReg + ".z\n" + 	// (1, 0)
 70				"tex " + depthCol + ", " + uvReg + ", " + depthMapRegister + " <2d, nearest, clamp>\n" +
 71				"dp4 " + depthCol + ".z, " + depthCol + ", " + decReg + "\n" +
 72				"slt " + uvReg + ".w, " + _depthMapCoordReg + ".z, " + depthCol + ".z\n" +   // 0 if in shadow
 73				
 74				"mul " + depthCol + ".x, " + _depthMapCoordReg + ".x, " + customDataReg + ".y\n" +
 75				"frc " + depthCol + ".x, " + depthCol + ".x\n" +
 76				"sub " + uvReg + ".w, " + uvReg + ".w, " + uvReg + ".z\n" +
 77				"mul " + uvReg + ".w, " + uvReg + ".w, " + depthCol + ".x\n" +
 78				"add " + targetReg + ".w, " + uvReg + ".z, " + uvReg + ".w\n" +
 79				
 80				"mov " + uvReg + ".x, " + _depthMapCoordReg + ".x\n" +
 81				"add " + uvReg + ".y, " + _depthMapCoordReg + ".y, " + customDataReg + ".z\n" +	// (0, 1)
 82				"tex " + depthCol + ", " + uvReg + ", " + depthMapRegister + " <2d, nearest, clamp>\n" +
 83				"dp4 " + depthCol + ".z, " + depthCol + ", " + decReg + "\n" +
 84				"slt " + uvReg + ".z, " + _depthMapCoordReg + ".z, " + depthCol + ".z\n" +   // 0 if in shadow
 85				
 86				"add " + uvReg + ".x, " + _depthMapCoordReg + ".x, " + customDataReg + ".z\n" +	// (1, 1)
 87				"tex " + depthCol + ", " + uvReg + ", " + depthMapRegister + " <2d, nearest, clamp>\n" +
 88				"dp4 " + depthCol + ".z, " + depthCol + ", " + decReg + "\n" +
 89				"slt " + uvReg + ".w, " + _depthMapCoordReg + ".z, " + depthCol + ".z\n" +   // 0 if in shadow
 90				
 91				// recalculate fraction, since we ran out of registers :(
 92				"mul " + depthCol + ".x, " + _depthMapCoordReg + ".x, " + customDataReg + ".y\n" +
 93				"frc " + depthCol + ".x, " + depthCol + ".x\n" +
 94				"sub " + uvReg + ".w, " + uvReg + ".w, " + uvReg + ".z\n" +
 95				"mul " + uvReg + ".w, " + uvReg + ".w, " + depthCol + ".x\n" +
 96				"add " + uvReg + ".w, " + uvReg + ".z, " + uvReg + ".w\n" +
 97				
 98				"mul " + depthCol + ".x, " + _depthMapCoordReg + ".y, " + customDataReg + ".y\n" +
 99				"frc " + depthCol + ".x, " + depthCol + ".x\n" +
100				"sub " + uvReg + ".w, " + uvReg + ".w, " + targetReg + ".w\n" +
101				"mul " + uvReg + ".w, " + uvReg + ".w, " + depthCol + ".x\n" +
102				"add " + targetReg + ".w, " + targetReg + ".w, " + uvReg + ".w\n";
103			
104			regCache.removeFragmentTempUsage(depthCol);
105			regCache.removeFragmentTempUsage(uvReg);
106			
107			vo.texturesIndex = depthMapRegister.index;
108			
109			return code;
110		}
111
112		/**
113		 * @inheritDoc
114		 */
115		override arcane function activateForCascade(vo:MethodVO, stage3DProxy:Stage3DProxy):void
116		{
117			var size:int = _castingLight.shadowMapper.depthMapSize;
118			var index:int = vo.secondaryFragmentConstantsIndex;
119			var data:Vector.<Number> = vo.fragmentData;
120			data[index] = size;
121			data[index + 1] = 1/size;
122		}
123
124		/**
125		 * @inheritDoc
126		 */
127		override arcane function getCascadeFragmentCode(vo:MethodVO, regCache:ShaderRegisterCache, decodeRegister:ShaderRegisterElement, depthTexture:ShaderRegisterElement, depthProjection:ShaderRegisterElement, targetRegister:ShaderRegisterElement):String
128		{
129			var code:String;
130			var dataReg:ShaderRegisterElement = regCache.getFreeFragmentConstant();
131			vo.secondaryFragmentConstantsIndex = dataReg.index*4;
132			var temp:ShaderRegisterElement = regCache.getFreeFragmentVectorTemp();
133			regCache.addFragmentTempUsages(temp, 1);
134			var predicate:ShaderRegisterElement = regCache.getFreeFragmentVectorTemp();
135			regCache.addFragmentTempUsages(predicate, 1);
136			
137			code = "tex " + temp + ", " + depthProjection + ", " + depthTexture + " <2d, nearest, clamp>\n" +
138				"dp4 " + temp + ".z, " + temp + ", " + decodeRegister + "\n" +
139				"slt " + predicate + ".x, " + depthProjection + ".z, " + temp + ".z\n" +
140				
141				"add " + depthProjection + ".x, " + depthProjection + ".x, " + dataReg + ".y\n" +
142				"tex " + temp + ", " + depthProjection + ", " + depthTexture + " <2d, nearest, clamp>\n" +
143				"dp4 " + temp + ".z, " + temp + ", " + decodeRegister + "\n" +
144				"slt " + predicate + ".z, " + depthProjection + ".z, " + temp + ".z\n" +
145				
146				"add " + depthProjection + ".y, " + depthProjection + ".y, " + dataReg + ".y\n" +
147				"tex " + temp + ", " + depthProjection + ", " + depthTexture + " <2d, nearest, clamp>\n" +
148				"dp4 " + temp + ".z, " + temp + ", " + decodeRegister + "\n" +
149				"slt " + predicate + ".w, " + depthProjection + ".z, " + temp + ".z\n" +
150				
151				"sub " + depthProjection + ".x, " + depthProjection + ".x, " + dataReg + ".y\n" +
152				"tex " + temp + ", " + depthProjection + ", " + depthTexture + " <2d, nearest, clamp>\n" +
153				"dp4 " + temp + ".z, " + temp + ", " + decodeRegister + "\n" +
154				"slt " + predicate + ".y, " + depthProjection + ".z, " + temp + ".z\n" +
155				
156				"mul " + temp + ".xy, " + depthProjection + ".xy, " + dataReg + ".x\n" +
157				"frc " + temp + ".xy, " + temp + ".xy\n" +
158				
159				// some strange register juggling to prevent agal bugging out
160				"sub " + depthProjection + ", " + predicate + ".xyzw, " + predicate + ".zwxy\n" +
161				"mul " + depthProjection + ", " + depthProjection + ", " + temp + ".x\n" +
162				
163				"add " + predicate + ".xy, " + predicate + ".xy, " + depthProjection + ".zw\n" +
164				
165				"sub " + predicate + ".y, " + predicate + ".y, " + predicate + ".x\n" +
166				"mul " + predicate + ".y, " + predicate + ".y, " + temp + ".y\n" +
167				"add " + targetRegister + ".w, " + predicate + ".x, " + predicate + ".y\n";
168			
169			regCache.removeFragmentTempUsage(temp);
170			regCache.removeFragmentTempUsage(predicate);
171			return code;
172		}
173	}
174}